Traversing 2D Arrays

Emily Wilson
11 min read
Listen to this study note
Study Guide Overview
This study guide covers 2D array traversal using nested for loops. It explains row-major and column-major traversals, including forward and reverse directions. It also covers standard algorithms applied to 2D arrays like searching, modifying values, putting values into other data structures (ArrayLists and Arrays), finding minimum/maximum, sum, mean, mode, checking properties, accessing consecutive sequences, and checking for duplicates.
Note: In this topic, we will be using arrayB and the graphical representation of 2D arrays from the previous topic. As a reminder, here is arrayB:
int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
#General Form
Recall that when traversing a 1D array, you use a loop, usually a for loop or enhanced for loop. This is similar to how we traverse 2D arrays, except in 2D arrays, we need 2 nested for loops, an outer loop traversing through one dimension, and an inner loop traversing through the other. Here is the general form:
for (firstDimension traversal conditions) { for (secondDimension traversal conditions) { System.out.print(item + " "); } } System.out.println();
#Row-Major vs. Column-Major
There are two main directions we can traverse through. The first is row-major traversal, traversing through every element in one row before moving to the next. Conversely, there is column-major traversal, traversing through every element in one column before moving on to the next.
#Row-wise Traversal
#Column-wise Traversal
We will focus on traversing in a forward direction (from left-to-right and from top-to-bottom) in this section. In the next, we will go in reverse directions, but this will only require a change in the for loop conditional.
#Row-Wise Traversal
When doing row-wise traversal, the outer loop traverses through the different rows, and the inner loop traverses through the elements in each row. Here is how you write code for this:
public static void rowWiseForward(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[0].length; j++) { System.out.print(array[i][j] + " "); } } }
public static void main(str[] args) { rowWiseForward(arrayB); }
1 2 3 4 5 6 7 8 9 10 11 12
We can only use nested enhanced for loops for forward row-wise traversals. Here is the code that creates the same output as above, only this time using enhanced for loops instead of regular for loops:
public static void rowWiseForwardEnhancedForLoops(int[][] array) { for (int[] row: array) { for (int number: row) { System.out.print(number + " "); } } }
Remember that the type of the rows in the 2D array are 1D arrays, and the rows contain elements of a certain type!
#Column-Wise Traversal
When doing column-wise traversal, the outer loop traverses through the different columns, and the inner loop traverses through the elements in each columns. Here is how you write code for this:
public static void columnWiseForward(int[][] array) { for (int i = 0; i < array[0].length; i++) { for (int j = 0; j < array.length; j++) { System.out.print(array[j][i] + " "); } } }
public static void main(str[] args) { columnWiseForward(arrayB); }
1 5 9 2 6 10 3 7 11 4 8 12
#In Summary
We can see that the two traversal algorithms using regular for loops are almost the same. The only significant difference is with the order of the for loop conditionals. However, the loop conditional for traversing through different rows is the same in both cases, and so is the conditional for traversing through different columns. Here are the conditiona...

How are we doing?
Give us your feedback and let us know how we can improve