2D Arrays: every key term you need (+ practice quiz)
51 flashcard terms for AP Computer Science A Unit 8, written to match the course framework. Read them here, drill them as flashcards, or take the 42-question quiz. Free, no account needed.
A 2D array is an array of row arrays: grid[r] is a 1D array (row r) and grid[r][c] an element. grid.length is the number of rows; grid[0].length the number of columns.
Row-major Traversal Order
Outer loop over rows, inner over columns visits elements left to right, top to bottom. This is the order the enhanced for loop for (int[] row : grid) for (int v : row) uses.
Column-major Traversal
Outer loop over columns (c < grid[0].length), inner over rows (r < grid.length), accessing grid[r][c]. Visits down each column before moving right.
Enhanced for over 2D Array
for (int[] row : grid) yields each row array; a nested for (int v : row) yields values. The row variable type must be int[] (or the element type's array).
Row Sum and Column Sum
Row sum: fix r, loop c. Column sum: fix c, loop r. Confusing which index is fixed is the top error in 2D FRQs.
Main Diagonal
Elements where row index equals column index: grid[i][i]. Only fully defined for square arrays.
Anti-diagonal
Elements where r + c == n - 1 for an n × n array: grid[i][n - 1 - i].
Neighbors of a Cell
Up/down change the row index, left/right change the column index. Always bounds-check r - 1 >= 0, r + 1 < grid.length, c - 1 >= 0, c + 1 < grid[0].length.
Elements where row + column equals n - 1, reached with grid[i][n - 1 - i] on a square grid.
Neighbor Bounds Checking
Before reading grid[r+dr][c+dc] verify that the new row and column are both within range, or edge cells will throw.
Counting Neighbors of a Cell
Interior cells have eight neighbors, edge cells five, and corner cells three when diagonals are included.
Checkerboard Parity
(r + c) % 2 partitions a grid into two alternating colors, which is the standard way to shade or filter a board.
Transpose Requires a New Array
On a non-square grid the result has swapped dimensions, so output[c][r] = input[r][c] must be written into a freshly allocated array.
In-place Square Transpose
Swapping grid[r][c] with grid[c][r] only for c > r transposes without a second array; looping over all pairs would undo every swap.
Total Iteration Count
A full nested traversal of an r-by-c grid executes the body r * c times, which is how growth is compared on the exam.
Searching Returns Two Coordinates
A 2D search must report both the row and the column, often by storing them in fields or returning early from a helper.
Row Sums vs Column Sums
A row sum fixes the outer index and accumulates across the inner one; a column sum needs the loops nested in the opposite order or an inner index that fixes the column.
2D Array of Objects
Every cell starts as null after allocation, so a nested loop must construct an object for each position before any method call.
Initializer Lists for Grids
int[][] g = {{1,2},{3,4}}; creates and fills the structure in one statement, with each inner brace list becoming one row.
Boundary Rows and Columns
Algorithms often treat the outer border separately, since those cells lack a full set of neighbors and skew averages.
Copying a 2D Array Deeply
Allocating a new outer array and copying each row's elements is required; copying only the row references produces shared rows.
Ragged Arrays Beyond the Subset
Java permits rows of different lengths, but the AP CED restricts questions to rectangular grids where grid[0].length describes every row.
Reading a Grid as Coordinates
Row index usually corresponds to a y or vertical position and column index to x, which is the reverse of typical math notation.