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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章