From the free course AP Computer Science A on Lykke
4.12 2D Array Traversals
A two-dimensional array traversal visits a grid systematically, usually one row at a time, so that every element is examined exactly as intended.
4.12 2D Array Traversals
A two-dimensional array traversal visits a grid systematically, usually one row at a time, so that every element is examined exactly as intended.
The grid-walking idea
Imagine a theater seating chart. The outer loop chooses a row; the inner loop walks across that row from the first seat to the last. If the chart has $3$ rows and $4$ columns, a row-major traversal visits positions in this order:
The row index is conventionally named r, and the column index c. For an array named scores, the element at row $r$ and column $c$ is written scores[r][c]. The outer loop must control rows, while the inner loop controls columns.
Row-major traversal with nested loops
The most common traversal uses two nested for loops. The expression scores.length gives the number of rows. Because each row is itself a one-dimensional array, scores[r].length gives the number of columns in the current row.
int[][] scores = {
{8, 6, 9},
{7, 10, 5},
{9, 8, 7}
};
int total = 0;
for (int r = 0; r < scores.length; r++) {
for (int c = 0; c < scores[r].length; c++) {
total += scores[r][c];
}
}
System.out.println(total);
To trace the code, the outer loop first sets r to 0. The inner loop adds 8, 6, and 9; then r becomes 1, and the inner loop adds 7, 10, and 5; finally, row 2 contributes 9, 8, and 7. The final value is $69$.
| Outer-loop value | Elements visited | Row total |
|---|---|---|
r = 0 |
8, 6, 9 |
$23$ |
r = 1 |
7, 10, 5 |
$22$ |
r = 2 |
9, 8, 7 |
$24$ |
Why the bounds matter
The correct bounds are usually r < scores.length and c < scores[r].length. Because indexes begin at $0$, the last valid row index is scores.length - 1, and the last valid column index in row r is scores[r].length - 1.
A frequent error is using scores.length for both loops. That works only when the number of rows happens to equal the number of columns. If scores has $4$ rows and $2$ columns, the inner loop would attempt to access columns 2 and 3, producing an ArrayIndexOutOfBoundsException.
Another error is using scores[0].length for every row. Java 2D arrays are arrays whose elements are rows, so rows can have different lengths. Using scores[r].length makes the traversal safe even for a ragged array.
Traversal patterns
Different tasks require different visit orders. The loop structure communicates the algorithm’s purpose:
A row-major traversal processes every element in row $0$, then every element in row $1$, and so on. A column-major traversal processes column $0$ across all rows, then column $1$, and so on; it is appropriate only when the rows provide the needed column.
for (int c = 0; c < scores[0].length; c++) {
for (int r = 0; r < scores.length; r++) {
System.out.println(scores[r][c]);
}
}
For a rectangular array, this column-major version is valid. For a ragged array, the inner loop must check whether row r actually contains column c before using scores[r][c].
Enhanced for loops
An enhanced for loop can make a row-major traversal easier to read when the code needs each value but does not need its indexes.
int total = 0;
for (int[] row : scores) {
for (int value : row) {
total += value;
}
}
Here, row refers to one one-dimensional row at a time, and value refers to each element in that row. The loop is excellent for calculating totals or finding a value, but ordinary indexed loops are required when the algorithm must modify a particular position or use the row and column indexes.
Misconception check
Misconception: “A 2D array has one universal column length.” In Java, array.length describes the number of row arrays. The expression array[r].length describes the length of one specific row. Confusing these two meanings is the main source of incorrect loop bounds.
Retrieval check
A 2D array has rows of lengths $4$, $2$, and $5$. Which expression should control the inner loop while processing row r, and why? The answer is array[r].length, because the valid column indexes depend on the particular row being visited. This row-and-column control is the foundation for the algorithms developed in CED Topic 4.13.

Source Materials
- AP Computer Science A Course and Exam Description
- AP Computer Science A Course and Exam Description Clarifications and Corrections
- AP Computer Science A Course Overview
- AP Computer Science A Course at a Glance
- AP Computer Science A Course at a Glance Poster
- Adopt AP Computer Science A
- AP Computer Science A
- AP Computer Science A