Skip to content

For Each

The "advanced for" or "foreach" loop is even better for iterating multidimensional arrays.

Remember we must always declare a local variable in the parentheses () of the loop.

int[] ints = {1, 2, 4, 5};
for (int local : ints) {
  // ...
}

The same is true for multidimensional array, but note the type of the local variable in the outer loop.

for (char[] row : board) {
  for (char element : row) {
    System.out.print(element); // print the element
  }
  System.out.println();
}

Foreach arrays

Drill

AdvancedArrays/com.example.advancedarrays.drills.MultiForEach * Add a method called printBands that takes one parameter and prints each array using nested foreach loops. * Call printBands from main.


Prev -- Up -- Next