📚Study Guide: 2D Array
Unit 8: 2D Array
Two-dimensional arrays extend the concept of arrays to store data in a grid or matrix format, with rows and columns. In Java, a 2D array is essentially an array of arrays, meaning each row is itself an array and can have a different length (ragged arrays), though the AP exam typically assumes rectangular arrays. Students must master nested loop traversal, understanding that the outer loop usually controls the row index and the inner loop controls the column index. Accessing elements uses two indices: arr[row][col]. The number of rows is arr.length; the number of columns in a rectangular array is arr[0].length. Common algorithms include row sums, column sums, finding max/min in the entire grid, diagonal traversal, and matrix transposition. The AP exam frequently presents free-response questions where students must implement methods that process 2D arrays, such as summing a row, averaging a column, or identifying elements that meet a condition. Understanding how to traverse only the main diagonal (where row == col) or the anti-diagonal is important. Students must also be able to initialize 2D arrays with literal values and understand that passing a 2D array to a method passes a reference to the array of row references.
Key Concepts
- Declaration and Initialization: int[][] grid = new int[3][4]; creates 3 rows, 4 columns. int[][] grid = {{1,2,3},{4,5,6}}; initializes with literals. Ragged arrays: each row can have different lengths.
- Indexing: grid[row][col]. Rows range 0 to grid.length-1. Columns range 0 to grid[0].length-1 (for rectangular arrays). Always check bounds before accessing.
- Traversal Patterns: Row-major: outer loop rows, inner loop columns (most common). Column-major: outer loop columns, inner loop rows. Diagonal: loop with single index i, access [i][i].
- Row and Column Operations: Sum a row by fixing the row index and looping over columns. Sum a column by fixing the column index and looping over rows. Finding row/column averages follows similar patterns.
- Enhanced for with 2D Arrays: for (int[] row : grid) { for (int val : row) { ... } }. This reads each row as an int[] array, then each value in that row.
Vocabulary
- Matrix: A rectangular arrangement of numbers or elements in rows and columns, represented as a 2D array.
- Row-Major Order: Traversing a 2D array by completing all columns in a row before moving to the next row.
- Column-Major Order: Traversing a 2D array by completing all rows in a column before moving to the next column.
- Main Diagonal: The set of elements in a square matrix where the row index equals the column index (i.e., [0][0], [1][1], [2][2]).
- Ragged Array: A 2D array in which each row has a different number of columns; also known as a jagged array.
- Transpose: An operation that flips a matrix over its main diagonal, swapping rows and columns.
Essential Formulas / Syntax Patterns
- int rows = grid.length;
- int cols = grid[0].length; // rectangular only
- for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[0].length; c++) { ... } }
- for (int r = 0; r < grid.length; r++) { // sum row r int sum = 0; for (int c = 0; c < grid[r].length; c++) sum += grid[r][c]; }
Common Mistakes
- Reversing Row and Column Indices: grid[row][col] is correct. grid[col][row] is only correct if you intentionally want to transpose or access column-major.
- Assuming All Rows Have Same Length: grid[0].length works for rectangular arrays but may fail for ragged arrays. Use grid[r].length inside the loop for safety.
- Off-By-One in Nested Loops: Ensure both loop conditions use <, not <=, when comparing to length. The last valid index is length - 1.
- Forgetting 2D Arrays Are Arrays of Arrays: grid.length is the number of rows. Each row is an array. You can swap entire rows by swapping row references: int[] temp = grid[i]; grid[i] = grid[j]; grid[j] = temp;
AP Exam Strategies
- Draw the Grid: Sketch the 2D array with row and column indices labeled before writing or tracing code.
- Identify Traversal Order: Determine whether row-major, column-major, or diagonal traversal is needed before writing loops.
- Check Rectangular Assumption: If the problem states "rectangular array," you can safely use grid[0].length for column count. Otherwise, use grid[r].length.
- Isolate Row or Column Logic: For row operations, fix the row index and vary the column. For column operations, fix the column and vary the row. Don't mix them up.
Real-World Applications
- Spreadsheets: Excel and Google Sheets are essentially 2D arrays of cells with formulas, formatting, and references.
- Digital Images: A grayscale image is a 2D array of pixel intensities; color images use 3D arrays (width x height x channels).
- Game Boards: Chess, checkers, and tic-tac-toe represent board states as 2D arrays where each cell holds a piece or empty marker.