Zachary Sweeney Zachary Sweeney - 7 months ago 16
Java Question

Using two for loops to loop through a 2D array in java

How do I use two for loops to loop through a 2D array?
for example-
1st for loop ---

for(int step = 0; step<array.length; step++){

2nd for loop ---

Jan Jan
Answer

As you have a 2-d array, you have two length values for each loop:

  //you have this? array[][];
  for(int i = 0; i < array.length; ++i) {
    for(int j = 0; j < array[i].length; ++j) {
      //do something on array[i][j] 
    }
  }