UNIT 4: DATA COLLECTIONS · TOPIC 4.11
4.11 2D Array Creation and Access
A 2D array is an array of arrays. Row first, then column — and each row is itself a 1D array you can pass around.
What you need to know
- Declare and create:
int[][] grid = new int[3][4];— 3 rows, 4 columns, all 0.grid.lengthis the number of rows;grid[0].lengthis the number of columns. - Initializer:
int[][] g = {{1, 2, 3}, {4, 5, 6}};— 2 rows, 3 columns. - Access:
grid[row][col].grid[1][2]is row 1, column 2 (both from 0). grid[r]by itself is a 1D array — row r. You can pass it to a method that takesint[].- On the AP exam, 2D arrays are rectangular — every row has the same length. (Java allows ragged arrays, but they aren't tested.)
- Default values follow 1D rules: 0, 0.0, false, null.
- Out of bounds in either dimension throws ArrayIndexOutOfBoundsException.
Worked example
int[][] m = {{1, 2, 3},
{4, 5, 6}};
System.out.println(m.length); // 2 (rows)
System.out.println(m[0].length); // 3 (columns)
System.out.println(m[1][2]); // 6
m[0][1] = 9; // row 0 becomes {1, 9, 3}
int[] secondRow = m[1]; // {4, 5, 6}
// m[2][0] // exception: no row 2
Exam tip: Read
m[a][b] as "row a, column b" every time — never reverse it. length counts rows; [0].length counts columns. A question that flips these is the standard distractor.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
int[][] g = new int[R][C]creates R row-arrays each of length C.gis an array of references to those rows.g.length= R.g[r].length= C for every r (rectangular).- Initializer:
{{1, 2}, {3, 4}, {5, 6}}— outer braces list rows, inner braces list each row's values. Count outer groups for rows, inner elements for columns. - Access
g[r][c]: first bracket row, second column. Always. The exam never varies this convention. g[r]is a 1D array — a real object you can pass to a method takingint[], iterate with enhanced for, or alias withint[] row = g[r];(changes torow[i]affectg[r][i]).- Defaults as for 1D. A
String[][]is all null. - Ragged arrays (rows of different lengths) are possible in Java but excluded from the CED. Assume rectangular; use
g[0].lengthfor column count. - Out of bounds in either dimension throws.
g[R][0]— no row R.g[0][C]— no column C. - Mental model: a table. Rows go down, columns go across. Row index first because you pick the row, then the position within it.
Mistakes that cost points
- Swapping row and column.
g[c][r]when you meang[r][c]. Read as row-then-column every time. - g.length for columns. It's rows. Columns is
g[0].length. - Counting inner elements as rows. In the initializer, outer braces = rows.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 After
double[][] t = new double[4][6];, what are t.length and t[0].length?Show answer
Answer: A. First dimension is rows (4), second is columns (6).
Q2 What is printed by the following code?
int[][] a = {{2, 4}, {6, 8}, {10, 12}};
System.out.println(a[2][0] + a[0][1]);Show answer
Answer: A. a[2][0] is 10 (row 2, col 0); a[0][1] is 4. 10 + 4 = 14.
Key vocabulary
- 2D array
- an array whose elements are arrays; rows of columns
- Row-major
- the convention that the first index is the row
- Rectangular array
- a 2D array where every row has the same length