use of foreach in Java
The Java5 new foreach syntax, which can be viewed as an extension of the For loop, is primarily useful in making it easier to traverse arrays or other collections (collections). This new syntax is named enhanced for or Foreeach (refer to the naming method for other languages).
foreach can be used to traverse each successive value in the array and collections. Of course it can also be used to traverse objects that have implemented iterable<e> interfaces (the need to define the iterator () function), while many collections classes have already implemented ITERABLE<E> The foreach syntax is useful.
General Format
The following are the use forms for foreach and for, with two basic equivalents, whether array or other iteration object.
example-computes the elements in the array and
foreach Syntax implementation:?
1 2 3 4 5 |
double [] ar = {1.2, 3.0, 0.8}; int sum = 0; for (double d:ar) {//D gets successively per value in AR. sum = D; } |
Implementation for syntax:?
1 2 3 4 5 |
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]; } |
when to use foreach Fit
As you can see from the example above, foreach can sometimes make the code clean, but it needs to be noted that some cases are not appropriate: Read only: The elements accessed cannot be assigned a single structure: it is impossible to traverse two structures at the same time, such as comparing two single elements of an array: only for a single element read One-way: Java5 support only for iterations of a single forward element: Do not use this syntax before JAVA5
the use of foreach in two-dimensional arrays
Example:?
1 2 3 4 5 6 7 8 9 |
public class test02 { public static void Main (String args[]) { int [] [] a = new int [] { {1, 2, 3, 4, 5}, {One, one} }; for (int [] row:a) { & nbsp; for (int element:row) System.out.print (element + ""); System.out.println (); } }} |
foreach more Use
As already mentioned, foreach is suitable for objects that implement the Iterable<t> interface, such as iterating over arrays or collections. such as traversing the list or set:?
1 2 3 4 5 6 7 8 9 10 11 12-13 |
List public void Go (list<string> List) {for (String element:list) {System.out.println (element); }//set public void Go (set<string> set) {for (String element:set) {System.out.println (ele ment); } } |
Collections traversal is as follows:?
1 2 3 4 5 |
public void Go (collection<string> Collection) {for (String element:collection) {System.out.println (element); } } |
In fact, when compiling, the compiler will expand the foreach, such as the collections above expanded as follows:?
1 2 3 4 5 6 |
public void Go (Collection Collection) {String s; for (Iterator iterator = Collection.iterator (); Iterator.hasnext (); System.out.println (s)) s = (String) iterator.next (); } |
Reproduced from: http://www.leyond.info/usage-of-foreach-in-java/#more-131521
More mysterious information about foreach,
Reference: http://caterpillar.onlyfun.net/Gossip/JavaEssence/Foreach.html, this article also refers to: http://yczhuang.blogspot.com/2009/02/ Javaforeach.html http://leepoint.net/notes-java/flow/loops/foreach.html