AP Computer Science A Unit 8: 2D Array
Study 2D array traversal, row-major order, nested loops, image processing with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
A 2D array is an array of arrays, representing data in rows and columns. This unit covers creation, traversal with nested loops, and algorithms that process grid-like data.
Why it matters
The AP CSA exam consistently includes a free-response question involving 2D arrays. Image processing, game boards, and spreadsheet-like data all use 2D arrays. Mastering row-major and column-major traversal is essential.
Key concepts
- A 2D array is declared as int[][] grid = new int[rows][cols] and accessed with grid[r][c].
- grid.length gives the number of rows; grid[0].length gives the number of columns.
- Row-major traversal processes all columns in each row before moving to the next row.
- Column-major traversal processes all rows in each column before moving to the next column.
2D Array Creation and Access
A 2D array is an array whose elements are themselves arrays. Declare and create with int[][] matrix = new int[3][4] for a 3-row, 4-column grid. Initialize with values using nested braces: int[][] grid = {{1,2},{3,4},{5,6}}. Access element at row r, column c with grid[r][c]. The number of rows is grid.length. The number of columns in row r is grid[r].length. Rows are indexed from 0 to grid.length - 1, and columns from 0 to grid[r].length - 1.
Traversing 2D Arrays
Nested for loops are the standard way to traverse a 2D array. Row-major order uses the outer loop for rows and inner for columns: for (int r = 0; r < grid.length; r++) for (int c = 0; c < grid[r].length; c++). Column-major reverses the nesting. An enhanced for loop can traverse row by row: for (int[] row : grid) for (int val : row). The choice between row-major and column-major depends on the problem. The AP exam expects you to implement both.
2D Array Algorithms
Common algorithms include summing all elements, finding the maximum in each row or column, counting elements that meet a condition, and checking whether two 2D arrays are equal element by element. More complex algorithms involve processing adjacent cells (neighbors) for tasks like image processing or game board analysis. When accessing neighbors, always check that indices are within bounds to avoid ArrayIndexOutOfBoundsException. These boundary checks are a frequent source of errors and exam questions.
AP exam tip
When processing neighbors of grid[r][c], check bounds before accessing: r-1 >= 0, r+1 < grid.length, c-1 >= 0, c+1 < grid[r].length. Forgetting these checks is a common error on the free response.
Connections to other units
- Unit 3: Nested loops from the iteration unit are the primary tool for 2D array traversal.
- Unit 5: A 2D array is technically an array of 1D arrays, extending concepts from the array unit.
- Unit 10: Recursive algorithms can process 2D arrays for tasks like flood fill.