Topic 4.12 Notes – 2D Array Traversals
1. What 2D Array Traversal Is
A 2D array looks like a grid, but in Java it’s actually an array of 1D arrays.
If you declare:
int[][] matrix;
Then:
matrix.length→ number of rowsmatrix[row].length→ number of columns in that row- Access is always
matrix[row][col]
Think of it like this:

In the highlighted example, matrix[1][2] means row 1, column 2.
Notice:
- First index = row
- Second index = column
- Rows can have different lengths (jagged arrays), so always use
matrix[row].length, notmatrix[0].length.
Traversal just means visiting elements in a specific order using nested loops.
2. The Three Main Traversal Orders
a. Row-Major Order (most common)
You finish an entire row before moving to the next.
Pattern:
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix[row].length; col++) {
System.out.print(matrix[row][col] + " ");
}
}
Flow looks like this, moving left to right across each row before dropping down:

This is the default for most FRQs. If nothing else is specified, assume row-major.
Key idea:
- Outer loop → rows
- Inner loop → columns
b. Column-Major Order
You finish an entire column before moving to the next.
for (int col = 0; col < matrix[0].length; col++) {
for (int row = 0; row < matrix.length; row++) {
System.out.print(matrix[row][col] + " ");
}
}
The access is still matrix[row][col].
Only the loop order changes.
Now the movement goes top to bottom down each column before shifting right:

Be careful: column-major assumes every row has that column. If the array is jagged, this can break.
c. Custom or Conditional Traversal
Sometimes you only visit certain elements.
Examples:
- Main diagonal →
matrix[i][i] - Anti-diagonal →
matrix[i][matrix.length - 1 - i] - Border elements only
- Neighbor checking
Still nested loops, but with:
- Modified bounds
ifconditions inside
These are common on FRQs when the question says “only adjacent cells” or “only diagonal elements.”
3. Enhanced For Loops with 2D Arrays
Because a 2D array is an array of 1D arrays, enhanced for loops work naturally.
for (int[] row : matrix) {
for (int value : row) {
System.out.println(value);
}
}
Important:
- Outer variable type =
int[](a row) - Inner variable type =
int(an element)
Great for:
- Summing
- Counting
- Searching
Not good when:
- You need row/column indices
- You need to modify elements
This does NOT modify the array:
for (int[] row : matrix) {
for (int value : row) {
value = 10; // does nothing to matrix
}
}
The loop variable is a copy. This is a classic multiple choice trap.
4. Applying Traversal on the Exam
Writing Methods (FRQ)
Most 2D FRQs follow this pattern:
- Choose traversal type (usually row-major).
- Use correct bounds:
- Rows →
matrix.length - Columns →
matrix[row].length
- Rows →
- Combine traversal with logic (sum, compare, count, etc.).
If they ask for a position like [r][c], you must use traditional loops.
Tracing Nested Loops (MC)
When tracing:
- Remember the inner loop resets every time the outer loop increments.
- Total iterations = rows × columns (if rectangular).
If it’s a 3×4 array, inner loop runs 4 times per row → 12 total executions.
Boundary Checking
When checking neighbors:
if (row >= 0 && row < matrix.length &&
col >= 0 && col < matrix[row].length)
Never assume:
- Square array
- Equal row lengths
- Safe neighbors at edges
Out-of-bounds errors are very common in neighbor problems.