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:
[Java]View Plaincopyprint?
- 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 that the T here is a temporary variable value that holds the array value that is currently traversed and cannot be traversed if you need to change the value of the array)
[Java]View Plaincopyprint?
- 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:
[Java]View Plaincopyprint?
- 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 ();
- }
- }
- }
Java for loop use method