This evening was determined to get dizzy, the hands of the smoke, slightly trembling.
foreach Summary:
foreach is a new feature of JAVA5, some people say it is copied C #, I am not sure, anyway, will be used, the main enhancement for the loop, in the traversal of arrays and collections is very convenient.
foreach is a simplified version of A For loop, but foreach does not completely replace the for loop, but any foreach can be rewritten as a For loop, and foreach is not a keyword, mainly the format, and the meaning of each for.
foreach statement format:
for (element type element variable value: Traverse object obj) {
A Java statement referencing x;
}
Take a look at some of the code below to see how powerful and simple Foeeach are:
1 PackageCn.zpoor;2 3 Importjava.util.List;4 Importjava.util.ArrayList;5 Importjava.util.Arrays;6 7 /**8 * @authorSchrödinger's Cat9 * foreach iterates through arrays and collections*/Ten Public classTestarray { One A Public voidInitarray () { - //definition and initialization of an array - intArr[] = {2,5,3}; theSYSTEM.OUT.PRINTLN ("-----One-dimensional array before sorting:"); - for(intX:arr) { -System.out.print (x + "");//The Foreach loop enters the value of the array element one by one - } + System.out.println (); - + //Array Bubble Sort A Arrays.sort (arr); at //array elements After the foreach output is sorted -SYSTEM.OUT.PRINTLN ("-----One-dimensional array after sorting:"); - for(intX:arr) { -System.out.print (x+ ""); - } - System.out.println (); in } - to //Collection Conversion Array + Public voidListtoarray () { - //Create list and add elements thelist<string> list =NewArraylist<string>(); *List.add ("1"); $List.add ("2");Panax NotoginsengList.add ("5"); - the //foreach Loop output collection element +System.out.println ("-----foreach Output Collection Element"); A for(String x:list) { theSystem.out.println (x+ ""); + } - System.out.println (); $ $ //convert ArrayList to an array -object[] obj =List.toarray (); -SYSTEM.OUT.PRINTLN ("-----array elements from the foreach output collection"); the for(Object x:obj) { -System.out.print (x.tostring () + "");//output The value of a numeric element individuallyWuyi } the System.out.println (); - } Wu - //foreach output two-dimensional array About Public voidTwodimensionarray () { $ intArr[][] = {{1,2},{3,4}}; -System.out.println ("-----foreach output two-dimensional array"); - for(int[] x:arr) { - for(inte:x) { ASystem.out.print (e+ "");//outputting array elements One by one, the essence of multidimensional arrays is that there is no multidimensional array + } the } - System.out.println (); $ } the the //foreach output three-dimensional array the Public voidThreedimensionarray () { the intArr[][][] = { -{{1,2},{3,4}}, in{{5,6},{7,8}} the }; the AboutSystem.out.println ("-----foreach output three-dimensional array"); the for(int[] x:arr) { the for(int[] y:x) { the for(intz:y) { +System.out.print (z + "");//Triple Loop, a little dead. - } the }Bayi } the } the - Public Static voidMain (string[] args) { -Testarray test =NewTestarray (); the Test.initarray (); the Test.listtoarray (); the Test.twodimensionarray (); the Test.threedimensionarray (); - } the } the the /*-----A one-dimensional array before sorting:94 2 5 3 the -----A sorted one-dimensional array: the 2 3 5 the -----The foreach Output collection element98 1 About 2 - 5101 102 array elements that are converted-----The foreach output collection103 1 2 5104 -----foreach output two-dimensional array the 1 2 3 4106 -----foreach output three-dimensional array107 1 2 3 4 5 6 7 8108 */
Limitations of foreach:
If you reference an array or an index of a collection, foreach cannot do so, because foreach simply iterates through an array or a collection.
Give me a chestnut:
1 PackageCn.zpoor;2 /**3 * @authorSchrödinger's Cat4 * Limitations of foreach*/5 Public classTestforeach {6 Public Static voidMain (string[] args) {7 //define an array8 int[] arr =New int[4];9SYSTEM.OUT.PRINTLN ("array defined before output assignment");Ten One for(intX:arr) { A System.out.println (x); - } - the //assigning values by index -System.out.println ("Assigning a value to a defined array"); - for(inti = 3;i>0;i--) { -Arr[i] =i; + } - + //array after the output assignment ASystem.out.println ("One-dimensional array after the Foreach output assignment"); at for(intX:arr) { - System.out.println (x); - } - } - } - in /*array defined before output assignment - 0 to 0 + 0 - 0 the assigning values to a defined array * one-dimensional array after the foreach output is assigned $ 0Panax Notoginseng 1 - 2 the 3 + */
Conclusion:
foreach is an enhanced version of the For loop, simplifying programming and improving the readability and security of the code (array out of bounds). In the case of an array or a collection index, foreach is not a good use. And foreach is generally used in conjunction with generics.
Java-foreach Analysis Summary