AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/4/4-11)
Unit 4 · Topic 4.11
4.11 2D Array Creation and Access
A 2D array stores data in rows and columns, like a spreadsheet or a game board. This topic covers creating 2D arrays, getting and setting elements with [row][col], finding the number of rows and columns, and the idea that a 2D array is really an array of arrays.
Key terms
- 2D array
- row
- column
arr[row][col]- row and column length
An array of arrays
Java stores a 2D array as an array whose elements are themselves arrays: each element of the outer array is one row. That's why so much of what you know about 1D arrays carries over. The size is set when the array is created and can't change, and the elements can be primitives or object references.
On the exam, every 2D array is rectangular: every row has the same number of columns.
A 2D array of objects works the same way. In a String[][] made with new, every element starts as null. Once filled in, names[r][c] is a reference you can call methods on, like names[r][c].length(). Free-response questions often use a 2D array of objects from a class the question describes, so you'll write things like grid[r][c].getValue().
Creating a 2D array
With new, give the number of rows, then the number of columns: int[][] seats = new int[3][4]; makes 3 rows and 4 columns, 12 elements in all. Every element starts at the default value: 0, 0.0, false or null.
With an initializer list, write a list of rows, each one an initializer list itself: int[][] marks = { {90, 85, 70}, {60, 75, 80} }; makes 2 rows and 3 columns.
Accessing elements
grid[row][col] reads or changes one element. The first index is always the row and the second is the column. Rows are numbered from 0 at the top and columns from 0 at the left.
grid.length is the number of rows. grid[0].length, the length of the first row, is the number of columns. Valid row indexes go from 0 to grid.length - 1 and valid column indexes from 0 to grid[0].length - 1. Anything outside those ranges throws an ArrayIndexOutOfBoundsException.
Getting a whole row
With just one index, grid[r] is the entire row r, a 1D array. You can pass it to a method that expects a 1D array or traverse it with a single loop. It's a reference to the real row, so changing it changes the 2D array:
int[][] marks = { {90, 85, 70}, {60, 75, 80} };
int[] second = marks[1];
second[0] = 100;
System.out.println(marks[1][0] + " " + second.length);
This prints 100 3: second and marks[1] are the same row. There's no matching shortcut for a column; to work with a column, loop down the rows.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Creating and indexing
What does this code print?
int[][] seats = new int[3][4]; seats[0][1] = 5; seats[2][3] = seats[0][1] + 2; int[][] marks = { {90, 85, 70}, {60, 75, 80} }; System.out.println(seats.length + " " + seats[0].length); System.out.println(seats[2][3] + " " + seats[1][1]); System.out.println(marks[1][0] + " " + marks[0][marks[0].length - 1]);Show the solutionHide the solution
- Step 1:
seatshas 3 rows and 4 columns, all starting at 0.seats.lengthis 3 andseats[0].lengthis 4. - Step 2:
seats[0][1]becomes 5, thenseats[2][3]becomes 5 + 2 = 7.seats[1][1]was never set, so it's still 0. - Step 3: In
marks, row 1 is {60, 75, 80}, somarks[1][0]is 60. - Step 4:
marks[0].length - 1is 2, somarks[0][2]is the last element of row 0, 70.
Answer: Three lines:
3 4,7 0,60 70. - Step 1:
- Example 2
Rows versus columns
marksis{ {90, 85, 70}, {60, 75, 80} }. What happens withmarks[2][0], and how do you get the 85?Show the solutionHide the solution
- Step 1:
markshas 2 rows, so valid row indexes are 0 and 1.marks[2][0]asks for row 2, which doesn't exist, so it throws anArrayIndexOutOfBoundsException. - Step 2: 85 is in row 0 (the first row) and column 1 (the second column), so it's
marks[0][1]. - Step 3: A common slip is writing
[col][row]. Always put the row first.
Answer:
marks[2][0]throws anArrayIndexOutOfBoundsException; the 85 ismarks[0][1]. - Step 1:
Common mistakes
- Swapping the indexes. It's
[row][col], so the vertical position comes first. - Using
grid.lengthfor the number of columns. That's the number of rows; columns aregrid[0].length. - Thinking
grid[r]is a copy of the row. It's a reference to the same row.
On the exam
- Free-response Question 4 is about 2D arrays and is worth 6 points since the 2025 update (it used to be 9). Expect a 2D array of objects or numbers and a method that reads or changes it.
Connected topics
Videos
Check yourself
4 questions on 4.11 2D Array Creation and Access. Pick an answer to see if you got it, and why.
Consider the following code segment.int[][] grid = new int[3][5];
grid[2][4] = 8;
System.out.println(grid.length + " " + grid[0].length + " " + grid[1][4]);What is printed as a result of executing the code segment?
Consider the following code segment.int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[1][0] + m[0][2]);What is printed as a result of executing the code segment?
Consider the following code segment.int[][] m = {{1, 2, 3}, {4, 5, 6}};
int[] row = m[1];
row[0] = 9;
System.out.println(m[1][0] + " " + row.length);What is printed as a result of executing the code segment?
Consider the following code segment.int[][] m = {{1, 2, 3}, {4, 5, 6}};
System.out.println(m[2][0]);What is printed as a result of executing the code segment?
0 of 4 answered