ForEach 的使用

基于PyTorch构建CNN实现CIFAR10图像分类:从模型搭建到准确率提升 深度学习在计算机视觉领域中的核心任务之一是图像分类,而卷积神经网络(CNN)正是解决这类问题的经典模型。理解CNN的工作原理,从卷积层、池化层到全连接层的设计,是掌握现代视觉识别技术的基础。在实际工程中,数据预处理、模型结构、训练策略共同决定最终的识别效果。以CIFAR10数据集为例,它包含十类彩色图片,是验证CNN性能的常用基准。通过PyTorch框架,可以快速搭建并训练一个分类模型,同时结合数据增强、批量归一化、Dropout等技巧,有效缓解过拟合,提升模型泛化能力。这一实践过程不仅适用于学术研究,也为 阅读详情

ForEach 是一个循环结构,但是却不像 for 一样使用下标。

它用于 数组 和 集合 的遍历。

1、数组的遍历

int[] arr = new int[]{1, 2, 3, 4};
foreach(int element in arr)
{
     Console.WriteLine(element);
}

2、集合的遍历

using System.Collections;
...

ArrayList list = new ArrayList();
list.Add("hello");
list.Add("guys");

foreach(string e in list)
{
     Console.WriteLine(e);
}

3、泛型集合的遍历

using System.Collections.Generic;
...

List<int> list = new List<int>();
list.Add(1);
list.Add(3);
list.Add(5);

foreach(int e in list)
{
     Console.WriteLine(e);
}

 

注意:虽然,ForEach 和 For 一样都是循环结构,但是,它们之间比较大的区别就在于,在Foreach内部是不能对 元素进行修改的,否则会发生编译错误。

ForEach中的变量是ReadOnly变量。

foreach(int e in list)
{
     if(e == 3)
     {
           e = 6; //导致编译错误,不能修改,因为是 只读  变量 
     }
}


如果,想要对遍历 数组或集合中的数据进行修改的话,就 改用 for 循环吧!

 

有时,我们不仅希望能够在 数组 和集合中使用 Foreach 循环,我们还希望对我们自定义的类使用ForEach循环。

什么意思呢?就是,对自定义的类的使用 Foreach循环,获取内部的元素。

 

这时,对于自定义的类,需要去继承 IEnumerable 或者 IEnumerable<T> 接口,就可以了。

IEnumetable 接口中 只有一个方法IEnumerator GetEnumerator()

由于返回值为 IEnumerator 意味着,我们还需要去 定义一个类 继承IEnumerator 接口。

IEnumerator 是所有非泛型枚举数的基接口。枚举数可用于读取集合中的数据,但不能用于修改基础集合

在IEnumerator中,有三个东西 需要我们去实现,分别是 bool MoveNext(), void Reset(), object Current.

 

public class Person
    {
        string name;
        int age;

        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        public override string ToString()
        {
            return this.name + " " + this.age;
        }
    }

    public class People : IEnumerable
    {
        private Person[] people;

        public People(Person[] _people)
        {
            people = new Person[_people.Length];

            for (int i = 0; i < _people.Length; i++)
            {
                people[i] = _people[i];
            }
        }

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)new PeopleEnumerator(this);
        }

        public class PeopleEnumerator : IEnumerator
        {
            private People p;
            private int position = -1;

            public PeopleEnumerator(People p)
            {
                this.p = p;
            }

            public bool MoveNext()
            {
                if (position < p.people.Length - 1)
                {
                    position++;
                    return true;
                }
                else 
                {
                    return false;
                }
            }

            public void Reset()
            {
                position = -1;
            }

            public object Current
            {
                get { return this.p.people[position]; }
            }
        }

        static void Main()
        {
            Person[] Persons = new Person[] { new Person("kim", 20), new Person("tim", 23) };
            People p = new People(Persons);

            foreach (object obj in p) {
                Person o = (Person)obj;
                Console.WriteLine(o);
            }

            Console.ReadKey(true);
        }
    }

 

其实,我们不用实现 IEnumerable 接口也能够使用 Foreach循环。只要在 类中有 GetEnumerator()方法和一个具有

MoveNext()、Reset()、Current 这三个东西的类,同样能够能行!

代码示例:

public class Person
    {
        string name;
        int age;

        public Person(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        public override string ToString()
        {
            return this.name + " " + this.age;
        }
    }

    public class People
    {
        private Person[] people;

        public People(Person[] _people)
        {
            people = new Person[_people.Length];

            for (int i = 0; i < _people.Length; i++)
            {
                people[i] = _people[i];
            }
        }

        public PeopleEnumerator GetEnumerator()
        {
            return new PeopleEnumerator(this);
        }

        public class PeopleEnumerator
        {
            private People p;
            private int position = -1;

            public PeopleEnumerator(People p)
            {
                this.p = p;
            }

            public bool MoveNext()
            {
                if (position < p.people.Length - 1)
                {
                    position++;
                    return true;
                }
                else 
                {
                    return false;
                }
            }

            public void Reset()
            {
                position = -1;
            }

            public Person Current
            {
                get { return this.p.people[position]; }
            }
        }

        static void Main()
        {
            Person[] Persons = new Person[] { new Person("kim", 20), new Person("tim", 23) };
            People p = new People(Persons);

            foreach (Person mate in p){
                Console.WriteLine(mate);
            }

            Console.ReadKey(true);
        }
    }

上例中,没有再继承 IEnumerable 接口,但是得到了相同的结果。但是书上说,这个还能够改进,两者同时使用。

但是,我不太懂它的含义就,暂且搁置!


c#中Foreach详细用法 C#中Foreach详细用法讲述了Foreach用法,并以具体事例为基础。 立即下载

相关推荐

从婴儿学步到工业级应用:VLA的‘隐式标定‘原理通俗图解

本文深入解析VLA(Vision-Language-Action)模型的'隐式标定'原理,通过类比婴儿学习机制,揭示其如何替代传统手眼标定技术。文章对比了工业机器人传统标定与VLA数据驱动方法的差异,并可视化神经网络注意力机制,展示其在空间关系编码上的突破性进展。最后探讨了VLA在工业应用中的优势与混合架构解决方案。

pz890123456的博客 324

.NET学习之Foreach……

  对于.NET Framework中的foreach使用“算得上”是很熟练了,但今天看了书才发现自己对其一点也不了解……   对于.NET Framework提供的“标准”的集合类型可以直接使用foreach,如(Array、ArrayList、HashTable),除此之外,对于自定义的集合对象,也可以使其支持foreach的使用,只要实现IEnumerable接口即可(刚提到的几种集合类型...

weixin_30457465的博客 260

避坑指南:LISFLOOD-FP模型数据单位、参数文件与results文件夹结果解析全攻略

本文详细解析了LISFLOOD-FP洪水模拟模型的关键应用环节,包括数据单位校验、参数文件优化及结果文件解读与可视化。特别针对常见的单位混淆问题,提供了从DEM到边界条件的全面校验方法,并深入剖析了.par参数文件的配置技巧,帮助用户避免模型崩溃和结果失真。

weixin_42533622的博客 237

.NET(C#)遍历(for,foreach,while)字典(Dictionary)的几种方法

NET (C#) 中,Dictionary 是一种非常实用的集合类型,用于存储键值对的集合。遍历 Dictionary 的方法有多种,包括使用 for 循环、foreach 循环和 while 循环。使用 foreach 循环是遍历 Dictionary 中所有键值对最常见和最简单的方法。for 和 while 循环在遍历 Dictionary 时不是很常见,因它们需要通过索引访问元素,但在某些特定情况下可能会有用。

weixin_42098295的博客 3361

银河麒麟系统V10 sp1,安装wine 9.0

银河麒麟系统V10 sp1 x86_64,安装wine 9.0

daqinzl的专栏 1万+

LAMMPS实战:如何用ReaxFF力场模拟碳氢化合物热解(附完整脚本)

本文提供了一份使用LAMMPS软件中ReaxFF反应力场模拟碳氢化合物热解过程的完整实战指南。内容涵盖从环境准备、力场参数选择、能量最小化,到升温恒温动力学模拟的完整脚本编写,并介绍了基于轨迹文件的产物分析方法,帮助计算化学与材料科学研究者快速上手这一关键的分子动力学仿真技术。

soda5的博客 1099

.net foreach

.net core foreach

weixin_45394846的博客 246

Wireshark、Fiddler、Charles 三款抓包工具的本质差异与选型指南

抓包工具是网络调试与接口分析的基础能力,其核心在于理解OSI模型分层原理——不同工具工作在不同协议层级,解决不同维度问题。Wireshark面向网络层(L2/L3),可捕获原始数据帧与TCP握手细节,适用于DNS异常、丢包重传等底层问题定位;Fiddler和Charles则工作在应用层(L7),作为HTTP(S)代理实现HTTPS解密、请求改写与弱网模拟,是API调试与移动端测试的首选。技术价值体现在:精准匹配问题域(如证书固定需Fiddler脚本绕过,iOS调试依赖Charles证书信任链),避免‘用显微

weixin_28831341的博客 253

.net中for与foreach的比较及使用。

当for与数组用在一起的时候,如:    for(int i=0 ;i.net的编译器会有一个自动的优化,就是去获取该数组的一个总值array.count,并存放在内存中,无论你的数组是多大, array.count在loop的时候不用反复获取array的总数。重点是这个过程并不是很快,可能是有一定的安全性操作,毕竟这个放在内存中的值还要及时的清除掉。当数组总数很小的时候,这个获取总数的操作占遍历

gxtiou的专栏 2327

爬虫面试:关于爬虫破解验证码的13个经典面试题

​解释:验证码是一种用于区分人类用户和自动化程序(如爬虫)的技术,通常通过显示图形、文字或音频等方式,要求用户完成特定任务(如识别扭曲的字母、点击特定区域等)。​作用:防止恶意爬虫滥用网站资源、进行批量注册、登录破解等行为,保护网站的安全性和数据的完整性。

数据知道的博客 1万+

基于simulink 的不间断电源ups的项目实例

UPS系统的核心组件包括整流器、逆变器、电池管理系统和控制系统。- **Three-Phase Controlled Rectifier**:设置触发角为0度,用于控制整流器的输出电压。- **Three-Phase Inverter**:设置逆变器的输出频率为60Hz,用于将直流电转换为交流电。- **Control System**:编写MATLAB函数,用于监测市电状态并控制各模块的工作模式。- **Step**:设置阶跃时间,例如0.05秒,阶跃前后的值分别为0和1,用于模拟市电中断。

xiaoheshang_123的博客 711

.net 循环语句

1、for循环 语法: 表达式1一般为声明循环变量,记录循环的次数(int i=0;) 表达式2一般为循环条件(i<10) 表达式3一般为改变循环条件的代码,使循环条件终有一天不再成立(i++)。 执行过程:程序首先执行表达式1,声明了一个循环变量用来记录循环的次数, 然后执行表达式2,判断循环条件是否成立,如果表达式2返回的结果为true, 则执行循环体。当执行完循环体后,执行表达式3,...

肥陈 2126

18-XLINX的XC7A100T的BANK0引脚说明

DXP_0和 DXN_0是纯输入引脚,Xilinx Artix-7 FPGA 内部集成的一个片上监控模数转换器​ 的差分模拟输入对,模数转换器​的模拟信号输入端,将外部的模拟电压​ 采样并转换为内部的数字值,是一个单向的信号采集过程。它是与 VCCADC配对的关键引脚,共同为内部的精密模数转换器提供一个纯净、安静、独立的参考地平面。多FPGA同步配置: 在多FPGA系统中,主FPGA配置完成后,可以拉低从FPGA的 INIT_B使其等待,等所有FPGA都准备好后,再同时释放,实现同步启动。

weixin_44479192的博客 233

java foreach 使用

foreach语句使用总结   foreach语句是java5的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便。   foreach语句是for语句的特殊简化版本,但是foreach语句并不能完全取代for语句,然而,任何的foreach语句都可以改写为for语句版本。   foreach并不是一个关键字,习惯上将这种特殊的for语句格式称之为“foreac...

weixin_34380781的博客 411

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

foreach方法使用

用法 foreach方法主要是针对数组而言的,对数组中的每个元素可以执行一次方法 var array = ['a', 'b', 'c', 'e']; array.forEach((a)=> { console.log(a); }); 属性 foreach方法主要有三个参数,分别是数组内容、数组索引、整个数组 var array = ['a', 'b', 'c', 'e']; array.forEach((a,b,c)=> { console.log(a); console

我的博客 10万+

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

使用 boost.foreach foreach

使用 boost.foreach foreach 作者: 江淼 Blog: http://blog.youkuaiyun.com/jiangfriend 时间: 2007-7-28 14:56 用过别的语言的朋友一定深受foreach这个命令所带来方便,可惜C++并没提供,随着1.34.0 boost.foreach正式成为boost的一部分,而且boost.foreach相当的简单易用。从此c

江淼的专栏 8880

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Java8中的forEach使用

文章目录forEach1. forEach method2. Java 8 stream forEach example3. Java forEach examle using List4. Java forEach example using Map5. Create custom action(自定义操作) forEach Java中的forEach是一个用于迭代集合或流,且可以对迭代的每个元...

FEEL的博客 2万+

forEach的使用方法

forEach是ES5中操作数组的一种方法,主要功能是遍历数组; forEach有三个参数; [].forEach(function(value,index,arrSelf){ //value代表数组中的每一项; //index代表数组中每一项的索引; //arrSelf 代表数组本身; }) var arr = [2,5,4,9]; var sum = 0; arr.f

sunlizhen的专栏 1万+

mybatis foreach里用bind、foreach中使用bind的坑、foreach中动态生成表名

mybatis foreach里用bind、foreach中使用bind的坑、foreach中动态生成表名

weixin_49114503的博客 1404

Java8 forEach 使用

没有在优快云找到特别好的帖子,贴一个传送门 Java8 forEach 使用

SakuraA6的博客 201

QT中foreach的使用

QT中foreach的使用

my_angle2016的博客 1059

关于foreach的使用

      当然首先要说明一点,Java的foreach也是用for来实现的,之所以说foreach是为了与传统的for循环区分开。     在我做《JAVA编程思想》访问控制这一章习题8的时候,用到了这样的语句: static Connection[] ca = new Connection[3];{ for (x:ca) { x = Con

icesnows's blog 716

java的foreach使用

java的foreach使用@TOC java的foreach遍历 //foreach语句 for(类型 元素变量 : 遍历对象) 此时print输出i(元素变量)可输出对象的第i个元素 上图 public class HelloWorld { public static void main(String[] args) { // 定义一个整型数组,长度为10 int[]...

qq_45218502的博客 1466

foreach php,PHP中foreach()的使用方法

本篇文章主要介绍PHP中foreach()的使用方法,感兴趣的朋友参考下,希望对大家有所帮助。PHP 4 引入了 foreach 结构,和 Perl 以及其他语言很像。这只是一种遍历数组简便方法。foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误。有两种语法,第二种比较次要但却是第一种的有用的扩展。foreach (array_expression as $...

weixin_35391606的博客 553

java forEach使用

java forEach使用 foreach 是 Java 中的一种语法糖,目的是方便程序员开发和提高性能。其实就是编译期间以特定的字节码或特定的方式来对这些语法进行处理。 1.普通数组forEach用法 对于数组,foreach 循环实际上还是用的普通的 for 循环,怎么说foreach 循环就是for 循环 int[] arr= {1,2,3,4,5} //forEach写法(效果跟下面同等...

qq_43203949的博客 4万+
上一篇: int?, ??你接触过么?
下一篇: 对 readonly keyword 和 const keyword 的 见解
Rechard0121
博客等级 码龄15年 4粉丝 39原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值