The For loop in Java has a--foreach notation (typically used only to traverse the entire array, without worrying about cross-border issues).
1.1) General wording:
Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[] arr = {1,2,3,4,5};for (int i = 0; i < arr.length; ++i) {System.out.println (arr[i]);}}
1.2) foreach notation: (Note the
Tis a temporary variable value that is used to hold the array value that is currently traversed and cannot be traversed if the value of the array needs to be changed
Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[] arr = {1,2,3,4,5};for (int t: ARR) {System.out.println (t);}}}
2) Iterate through the two-dimensional array using foreach:
Package Foreach.main.sn;public class foreach {public static void main (string[] args) {int[][] arr2 = {{1,2,3},{4,5},{6,7,8 }};for (int t1[]: arr2) {for (int t2:t1) {System.out.print (t2);} System.out.println ();}}}
Original address: http://blog.csdn.net/qingdujun/article/details/40799561
Java for loop use method