FOR EACH

For-each Loop
Purpose
The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each (because it is called this in other programming languages). I’ve also heard it called the for-in loop.
Use it in preference to the standard for loop if applicable (see last section below) because it’s much more readable.
Series of values. The for-each loop is used to access each successive value in a collection of values.
Arrays and Collections. It’s commonly used to iterate over an array or a Collections class (eg, ArrayList).
Iterable. It can also iterate over anything that implements the Iterable interface (must defineiterator() method). Many of the Collections classes (eg, ArrayList) implement Iterable, which makes thefor-each loop very useful. You can also implement Iterable for your own data structures.
General Form
The for-each and equivalent for statements have these forms. The two basic equivalent forms are given, depending one whether it is an array or an Iterable that is being traversed. In both cases an extra variable is required, an index for the array and an iterator for the collection.

这里我们只要知道下面的事实就好了:

For-each语法内部,对collection是用nested iteratoration来实现的,对数组是用下标遍历来实现。

Java 5 及以上的编译器隐藏了基于iteration和下标遍历的内部实现。(注意,这里说的是“Java编译器”或Java语言对其实现做了隐藏,而不是某段Java代码对其实现做了隐藏,也就是说,我们在任何一段JDK的Java代码中都找不到这里被隐藏的实现。这里的实现,隐藏在了Java 编译器中,我们可能只能像这篇帖子中说的那样,查看一段For-each的Java代码编译成的字节码,从中揣测它到底是怎么实现的了)

下面对“For-each”和“其对等的iteration/index实现”的对比再简洁明了不过了。

For-each loop Equivalent for loop
for (type var : arr) {
body-of-loop
}
for (int i = 0; i < arr.length; i++) {
type var = arr[i];
body-of-loop
}
for (type var : coll) {
body-of-loop
}
for (Iterator iter = coll.iterator(); iter.hasNext(); ) {
type var = iter.next();
body-of-loop
}
Example - Adding all elements of an array
Here is a loop written as both a for-each loop and a basic for loop.
double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (double d : ar) { // d gets successively each value in ar.
sum += d;
}
And here is the same loop using the basic for. It requires an extra iteration variable.
double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (int i = 0; i < ar.length; i++) { // i indexes each element successively.
sum += ar[i];
}
Where the for-each is appropriate

【yasi】一定要注意For-each不是万能的,下面的场合是不适宜使用For-each的
Altho the enhanced for loop can make code much clearer, it can’t be used in some common situations.
使用For-each时对collection或数组中的元素不能做幅值操作
Only access. Elements can not be assigned to, eg, not to increment each element in a collection.
同时只能遍历一个collection或数组,不能同时遍历多余一个collection或数组
Only single structure. It’s not possible to traverse two structures at once, eg, to compare two arrays.
遍历过程中,collection或数组中同时只有一个元素可见,即只有“当前遍历到的元素”可见,而前一个或后一个元素是不可见的。
Only single element. Use only for single element access, eg, not to compare successive elements.
只能正向遍历,不能反向遍历(相比之下,C++ STL中还有reverse_iterator, rbegin(), rend()之类的东西,可以反向遍历)
Only forward. It’s possible to iterate only forward by single steps.
如果要兼容Java 5之前的Java版本,就不能使用For-each
At least Java 5. Don’t use it if you need compatibility with versions before Java 5.

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值