Unit 8: 2D Arrays

AP Computer Science A

What you'll master in this unit

Visualizing a 2D array

java
int[][] grid = {
    {1, 2, 3},    // row 0
    {4, 5, 6},    // row 1
    {7, 8, 9}     // row 2
};
java
grid[0][0]  // 1 (top-left)
grid[1][2]  // 6 (row 1, col 2)
grid[2][1]  // 8 (bottom-middle)

Key dimensions

java
grid.length       // 3 — number of rows
grid[0].length    // 3 — number of columns in row 0

Two traversal orders

java
// Row-major: left to right, top to bottom
for (int r = 0; r < grid.length; r++)
    for (int c = 0; c < grid[0].length; c++)
        // process grid[r][c]

// Column-major: top to bottom, left to right
for (int c = 0; c < grid[0].length; c++)
    for (int r = 0; r < grid.length; r++)
        // process grid[r][c]

Why Unit 8 matters