UNIT 4: DATA COLLECTIONS · TOPIC 4.12
4.12 2D Array Traversals
Nested loops visit every cell. Which loop is outer determines the order — and the exam asks about that order.
What you need to know
- Row-major traversal: outer loop over rows, inner over columns. Visits row 0 left to right, then row 1, etc. This is the default.
- Column-major traversal: outer loop over columns, inner over rows. Visits column 0 top to bottom, then column 1.
- Nested enhanced for:
for (int[] row : grid) { for (int v : row) { ... } }— row-major, read only. - Use
grid.lengthfor the row bound andgrid[0].length(orgrid[r].length) for the column bound. - Partial traversals: a single row (
grid[r][c]for c), a single column (grid[r][c]for r), the diagonal (grid[i][i]), or a sub-region. - Traversal order matters when output is printed in sequence or when elements are appended to a list.
Worked example
int[][] g = {{1, 2, 3},
{4, 5, 6}};
// row-major: 1 2 3 4 5 6
for (int r = 0; r < g.length; r++)
{
for (int c = 0; c < g[0].length; c++)
{
System.out.print(g[r][c] + " ");
}
}
// column-major: 1 4 2 5 3 6
for (int c = 0; c < g[0].length; c++)
{
for (int r = 0; r < g.length; r++)
{
System.out.print(g[r][c] + " ");
}
}
Exam tip: Look at which index the outer loop controls. Outer over r → row-major. Outer over c → column-major. When asked for the printed output, write out the grid and read it in that order.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Row-major (outer r, inner c) visits row 0 fully, then row 1, etc. Reading order for English text. Column-major (outer c, inner r) visits column 0 top to bottom, then column 1. The loop nesting order determines which.
- Nested enhanced for:
for (int[] row : g) for (int v : row)— row-major only, read-only. To modify cells you need indices. - Single row:
for (int c = 0; c < g[r].length; c++) g[r][c]. Single column:for (int r = 0; r < g.length; r++) g[r][c]. There's nog[][c]— a column has to be assembled by looping rows. - Diagonals: main diagonal
g[i][i]; anti-diagonalg[i][n - 1 - i]for a square grid. - Bounds: row bound is
g.length, column boundg[0].length(org[r].length). Swapping them works only for square grids and fails otherwise — the exam uses non-square grids to catch it. - Output order questions: write the grid, then read it in the traversal order (across rows or down columns). Trust the loop nesting, not the variable names.
- Traversal with a running index: when copying between a 1D array and a 2D array, keep a separate counter
kthat increments once per cell.
Mistakes that cost points
- Assuming the outer loop is always rows. Check which variable it controls.
- Swapping bounds on a non-square grid. Exception or missed cells.
- Trying to grab a column as an array. No such thing. Loop rows.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 What is printed by the following code?
int[][] m = {{1, 2}, {3, 4}, {5, 6}};
for (int c = 0; c < m[0].length; c++)
{
for (int r = 0; r < m.length; r++)
{
System.out.print(m[r][c]);
}
}Show answer
Answer: B. Column-major: column 0 (1, 3, 5) then column 1 (2, 4, 6).
Q2 Which of the following computes the sum of the elements on the main diagonal of a square 2D array
sq?Show answer
Answer: A. The main diagonal is where row == column: sq[i][i].
Key vocabulary
- Row-major traversal
- outer loop over rows, inner over columns
- Column-major traversal
- outer loop over columns, inner over rows