Lesson 4.11: 2D Array Creation and Access
Lesson 4.11: 2D Array Creation and Access
What You'll Learn
- 4.11.A: Represent collections of related data using 2D arrays.
- 4.11.B: Access and update individual elements of a 2D array.
- Declare and initialize 2D arrays using
newand initializer lists. - Use row and column indices correctly —
arr[row][col]. - Use
arr.lengthandarr[0].lengthto determine dimensions. - Identify the default values of a newly created 2D array.
Key Vocabulary
| Term | Definition |
|---|---|
| 2D array | An array of arrays — a rectangular grid of elements organized into rows and columns. |
| row | The first index in a 2D array access. arr[row][col] — row comes first. |
| column | The second index in a 2D array access. arr[row][col] — column comes second. |
| arr.length | For a 2D array, gives the number of rows. |
| arr[0].length | For a 2D array, gives the number of columns. |
What Is a 2D Array?
A 2D array is an array whose elements are themselves arrays — a grid of values organized into rows and columns. You can think of it exactly like a spreadsheet or a game board.
| col 0 | col 1 | col 2 | |
|---|---|---|---|
| row 0 | 1 | 2 | 3 |
| row 1 | 4 | 5 | 6 |
| row 2 | 7 | 8 | 9 |
This 3×3 grid has 3 rows and 3 columns. The element at row 1, column 2 is 6.
Declaring and Creating 2D Arrays (4.11.A)
Using new
int[][] grid = new int[3][4]; // 3 rows, 4 columns — all zeros
The first number is rows, the second is columns. All elements default to 0 (for int), false (for boolean), or null (for objects).
Using an Initializer List
int[][] scores = {
{90, 85, 92},
{78, 88, 95},
{65, 72, 80}
};
Each inner set of braces is one row. This creates a 3×3 array. Java infers the dimensions from the values.
Accessing and Updating Elements (4.11.B)
Access elements with two indices: arr[row][col]. Row always comes first.
int[][] scores = {
{90, 85, 92},
{78, 88, 95},
{65, 72, 80}
};
System.out.println(scores[0][2]); // 92 (row 0, col 2)
System.out.println(scores[1][0]); // 78 (row 1, col 0)
System.out.println(scores[2][1]); // 72 (row 2, col 1)
scores[1][1] = 99; // replaces 88 with 99
⚠️ AP Exam Trap: Row First, Column Second
arr[row][col] — always row first. This is the most common mix-up on the AP exam. If a question says "element at row 2, column 0," you write arr[2][0], not arr[0][2].
Getting the Dimensions
int[][] grid = new int[4][6]; // 4 rows, 6 columns
System.out.println(grid.length); // 4 — number of rows
System.out.println(grid[0].length); // 6 — number of columns
grid.length gives the number of rows because a 2D array is an array of row-arrays, and length counts how many rows there are. grid[0].length gives the length of row 0, which is the number of columns.
📌 Memorize This Pattern
arr.length → rows. arr[0].length → columns. These two expressions appear in every nested loop that traverses a 2D array. Getting them backwards produces the wrong loop bounds and causes out-of-bounds exceptions.
✅ Real-World Model: A Gradebook
// 3 students (rows), 4 test scores each (columns)
int[][] gradebook = {
{90, 85, 92, 88},
{78, 80, 75, 82},
{95, 91, 98, 94}
};
// Student 2's score on test 3:
System.out.println(gradebook[2][3]); // 94
Row = student, column = assignment. This is the canonical 2D array real-world model — students, scores, grids, seating charts, game boards all follow the same pattern.
Summary
- A 2D array is a grid: rows and columns. Declare as
int[][] arr = new int[rows][cols]. - Access with
arr[row][col]— row index first, column index second. -
arr.length= number of rows.arr[0].length= number of columns. - Default values are the same as 1D arrays: 0, false, or null depending on type.
- Initializer list syntax nests inner braces for each row.
Practice Questions
grid[1][2] after this declaration?
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
grid[1][2] = row 1, col 2 = 6.int[][] arr = new int[5][3], what is the value of arr.length and arr[0].length?new int[5][3] creates 5 rows and 3 columns. arr.length = 5 (rows). arr[0].length = 3 (columns in row 0). A has them swapped. B multiplies them (total elements, not dimensions).int[][] m = {
{10, 20},
{30, 40},
{50, 60}
};
System.out.println(m.length + " " + m[0].length);
m.length = 3, m[0].length = 2. Output: "3 2".data[0][2] after this declaration?
int[][] data = new int[3][4];
new and no initializer, all elements default to 0 for int. D would be correct for an object type like String.int[][] grid = new int[4][3];
grid[0][2] = 99;
grid[3][2] = 99;
grid[2][0] = 99;
grid[0][3] = 99;
grid[2][0]. A swaps the indices. B and D use out-of-range values for this 4×3 grid (valid rows: 0–3, valid cols: 0–2).Mastery: 2D Array Creation and Access
int[][] arr = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
System.out.println(arr[2][1] + arr[0][3]);
arr[2][1]: row 2 is {9,10,11,12}, col 1 = 10. arr[0][3]: row 0 is {1,2,3,4}, col 3 = 4. Sum = 10 + 4 = 14.arr.length
arr[0].length
arr.length[0]
arr[1].length + 1
arr.length gives rows. arr[0].length gives the length of the first row, which is the number of columns. C is invalid syntax — length is a field, not an array. D is off by one.int[][] board = new int[3][3]; board[0][0] = 1; board[1][1] = 2; board[2][2] = 3; System.out.println(board[0][1] + board[1][1] + board[2][1]);
board[0][1]=0 (default), board[1][1]=2 (set), board[2][1]=0 (default). Sum = 0+2+0 = 2.int[][] arr = new int[2][4];
int[][] arr = new int[4, 2];
int[][] arr = new int[4][2];
int[4][2] arr = new int[][];
new int[rows][cols] — first number is rows. A creates 2 rows and 4 columns (swapped). B uses a comma which is invalid Java syntax. D has the brackets in the wrong place and invalid initializer syntax.int[][] table = {
{2, 4, 6},
{8, 10, 12}
};
table[0][1] = table[1][2] - table[0][0];
System.out.println(table[0][1]);
table[1][2] = 12 (row 1, col 2). table[0][0] = 2 (row 0, col 0). 12 - 2 = 10. So table[0][1] is set to 10. Output: 10.Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]