📖 Crammy · All study guides
AP Computer Science A · Unit 8

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.

Study this unit free →

More AP Computer Science A guides

2D Arrays
Array of arrays; represents table/matrix. int[][] matrix = new int[rows][cols];. Access: matrix[i][j].
2D Array Initialization
int[][] matrix = {{1,2,3}, {4,5,6}};. Rows: matrix.length, columns: matrix[0].length (if rectangular).
2D Array Iteration
Nested loops: outer row, inner column. for(int i=0; i<matrix.length; i++) for(int j=0; j<matrix[i].length; j++).
Ragged Arrays
Each row different length. int[][] ragged = new int[3][]; ragged[0] = new int[2]; ragged[1] = new int[5];.
2D Array Algorithms
Row sum: sum column j for each i. Column sum: sum row i for each j. Diagonal: matrix[i][i]. Reverse: swap.
Matrix Operations
Addition: new[i][j] = a[i][j] + b[i][j]. Multiplication: complex (inner dimension must match).
Searching 2D Arrays
Linear search: nested loops checking each element. 2D index: track both i,j. Return found or not found.
2D Array Pass to Method
Method receives reference. Modifications affect original. Example: void fillMatrix(int[][] m) modifies array outside.
ArrayList of ArrayLists
ArrayList<ArrayList<Integer>> = dynamic 2D structure. Allows ragged dimensions. More flexibility than 2D array.
Unit 8 Summary
2D arrays represent tables/matrices. Nested loops iterate. Ragged arrays allow flexible dimensions. Support complex algorithms.
Row-major Storage
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.
Drill these as interactive flashcards →
Rectangular Assumption
The AP CED assumes 2D arrays are rectangular (all rows same length). Ragged arrays are legal Java but not tested.
Creating and Initializing
int[][] g = new int[3][4]; makes 3 rows of 4 zeros. int[][] g = {{1,2},{3,4}}; sets rows explicitly; the outer braces list rows.
Filling from a 1D Source
Element index k in row-major order maps to row k / cols and column k % cols. Useful for filling a grid from a list or String.
Searching a 2D Array
Nested loops return the position when found; to return both indices, return an int[] {r, c} or a small object, since methods return one value.
Row Reference Assignment
int[] r = grid[1]; aliases row 1; r[0] = 5 changes grid[1][0]. Rows are shared references, not copies.
Transposing
Result t[c][r] = grid[r][c] with dimensions swapped: new int[cols][rows]. Square matrices can be transposed in place by swapping above-diagonal pairs.
Nested Loop Iteration Count
A full traversal of an r × c array executes the inner body r × c times. Partial traversals (upper triangle) need the triangular formula.
Chess-board Parity
(r + c) % 2 == 0 identifies alternating cells; used in checkerboard patterns and diagonal-neighbor problems.
2D Arrays of Objects
String[][] board = new String[3][3]; starts as all null; each cell needs an assignment before method calls.
Row Reversal vs Whole Reversal
Reversing each row keeps rows in place but flips their contents; reversing the row ORDER swaps entire rows top to bottom.
Bounds in Nested Loops
Use grid.length for the row bound and grid[r].length (or grid[0].length) for the column bound. Swapping them fails on non-square arrays.
Array of Arrays Reality
A Java 2D array is an array whose elements are references to 1D arrays, which is why grid[r] is itself a usable array object.
grid.length vs grid[0].length
The first gives the number of rows and the second the number of columns in row 0; the AP subset assumes every row has the same length.
Row-major Visit Sequence
With rows outside and columns inside, cells are reached as (0,0), (0,1), (0,2), then (1,0), so each row finishes before the next begins.
Column-first Nesting
Placing the column loop on the outside walks straight down each column, which is what column totals and transposed printing require.
Enhanced for on a 2D Array
The outer enhanced for yields int[] rows and the inner yields individual elements, so neither loop provides the current indices.
Row Reference Assignment Aliases
grid[1] = grid[0]; makes both rows the same array object, so a later write to one appears in the other.
Main Diagonal Condition
Elements where the row index equals the column index; a single loop over i reaching grid[i][i] suffices on a square grid.
Test yourself on this unit →
Anti-diagonal Condition
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.
Turn these into flashcards & quizzes →