概述
参考:
http://blog.csdn.net/a596620989/article/details/6930479
http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work
http://www.leepoint.net/notes-java/flow/loops/foreach.html
以上几篇参考帖子中,这一篇讲的最简洁明了。摘录如下:
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<E>. It can also iterate over anything that implements the Iterable<E> interface (must defineiterator()
method). Many of the Collections classes (eg, ArrayList
) implement Iterable<E>, which makes thefor-each loop very useful. You can also implement Iterable<E> 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.
【yasi】这里我们只要知道下面的事实就好了:
- 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<type> 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
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.
- Only single structure. It's not possible to traverse two structures at once, eg, to compare two arrays.
- Only single element. Use only for single element access, eg, not to compare successive elements.
- Only forward. It's possible to iterate only forward by single steps.
- At least Java 5. Don't use it if you need compatibility with versions before Java 5.
最后
以上就是结实长颈鹿为你收集整理的Java For-each 的实现原理 For-each Loop的全部内容,希望文章能够帮你解决Java For-each 的实现原理 For-each Loop所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复