5m left·0%
Reading Time: 5 min
Last Updated: March 23, 2026
Main Ideas: 4
Reading Time: 5 min
Last Updated: March 23, 2026
Main Ideas: 4

Topic 4.12 Notes – 2D Array Traversals

Verified for 2027 AP® Computer Science A Exam
Read aloud
A 2D array is an array of arrays, and traversal means systematically visiting elements using nested loops. On the AP exam, this shows up heavily in FRQ 4 and in code-tracing multiple choice questions.

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 rows
  • matrix[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, not matrix[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
  • if conditions 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:

  1. Choose traversal type (usually row-major).
  2. Use correct bounds:
    • Rows → matrix.length
    • Columns → matrix[row].length
  3. 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.

Key Takeaways

Access order is always matrix[row][col], no matter the traversal style.
Use matrix[row].length, not matrix[0].length, to handle jagged arrays safely.
Row-major is the default traversal unless the problem clearly suggests columns.
Enhanced for loops cannot modify elements and do not give you indices.
The inner loop fully completes before the outer loop increments.
Neighbor problems require boundary checks every single time.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining