Dove's LibraryDove's Library

4.12 - 2D Array Traversals

Since a 2D array is really an array of arrays, you traverse it with nested loops, an outer loop and an inner loop.
You can go in row-major order (moving across each row first) or column-major order (moving down each column first).
int[][] grid = { {1, 2, 3}, {4, 5, 6} }; // row-major order (across each row) for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { System.out.println(grid[row][col]); } } // column-major order (down each column) for (int col = 0; col < grid[0].length; col++) { for (int row = 0; row < grid.length; row++) { System.out.println(grid[row][col]); } }
For this grid, row-major order prints 1, 2, 3, 4, 5, 6 and column-major order prints 1, 4, 2, 5, 3, 6.
In column-major order, the outer loop goes through the columns (up to grid[0].length) and the inner loop goes through the rows (up to grid.length), but the element is still grid[row][col]. Mixing up those bounds is a classic bug, and on a grid that isn’t square it throws an ArrayIndexOutOfBoundsException.
You can also make up your own order, like going backward or only visiting part of the grid:
int[][] grid = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // backward through everything, bottom row first: 9 8 7 6 5 4 3 2 1 for (int row = grid.length - 1; row >= 0; row--) { for (int col = grid[row].length - 1; col >= 0; col--) { System.out.print(grid[row][col] + " "); } } System.out.println(); // main diagonal, top-left to bottom-right (square grids only): 1 5 9 for (int i = 0; i < grid.length; i++) { System.out.print(grid[i][i] + " "); } System.out.println();
You can also use nested enhanced for loops. The outer variable represents each row (a 1D array), and the inner variable represents each individual element in that row.
for (int[] row : grid) { // outer variable is a whole row (int[]) for (int val : row) { // inner variable is a single int System.out.println(val); } }
Nested enhanced for loops always go in row-major order. For column-major order, or any other order, use indexed loops.
Same rule applies here too, reassigning the loop variable doesn’t change what’s actually stored in the array.
int[][] grid = { {1, 2, 3}, {4, 5, 6} }; for (int[] row : grid) { for (int val : row) { val = 0; // only changes the copy, grid is unchanged } } for (int[] row : grid) { for (int c = 0; c < row.length; c++) { row[c] = 0; // row is the real row, so grid really changes } } // grid is now { {0, 0, 0}, {0, 0, 0} }
The second version works because row holds a reference to an actual row of the 2D array, so changing row[c] changes the real element. Only assigning a new value to the loop variable itself has no effect.

AP® is a registered trademark of the College Board, which is not affiliated with Dove's Library and was not involved in the production of, and does not endorse, this website.

Providing free, high-quality AP notes to help students succeed, earn college credit, and reach their educational goals, ensuring everyone has access to the resources they need.

© 2026 Standard Learning Group, Inc., which operates Dove's Library. All rights reserved.