Lesson 4.11: 2D Array Creation and Access

Unit 4 · Lesson 4.11 · 2D Arrays

Lesson 4.11: 2D Array Creation and Access

🕑 35–45 min · 10 Practice Questions · New Syntax · Row/Column Model

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 new and initializer lists.
  • Use row and column indices correctly — arr[row][col].
  • Use arr.length and arr[0].length to 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

MCQ 1
What is the value of grid[1][2] after this declaration?
int[][] grid = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};
Predict before reading the options.
A 5
B 6
C 8
D 3
B. Row 1 is {4, 5, 6}. Column 2 of that row is 6. Remember: row first, column second. grid[1][2] = row 1, col 2 = 6.
MCQ 2
Given int[][] arr = new int[5][3], what is the value of arr.length and arr[0].length?
Predict before reading the options.
A arr.length = 3, arr[0].length = 5
B arr.length = 15, arr[0].length = 15
C arr.length = 5, arr[0].length = 3
D arr.length = 5, arr[0].length = 5
C. 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).
MCQ 3
What is printed?
int[][] m = {
    {10, 20},
    {30, 40},
    {50, 60}
};
System.out.println(m.length + " " + m[0].length);
Predict both values before reading the options.
A 2 3
B 6 6
C 2 2
D 3 2
D. Three rows (each inner array), two columns (each row has 2 elements). m.length = 3, m[0].length = 2. Output: "3 2".
MCQ 4
What is the value of data[0][2] after this declaration?
int[][] data = new int[3][4];
Predict before reading the options.
A 0
B 4
C 3
D null
A. When created with new and no initializer, all elements default to 0 for int. D would be correct for an object type like String.
MCQ 5
Which statement correctly updates the element at row 2, column 0 to the value 99?
int[][] grid = new int[4][3];
A grid[0][2] = 99;
B grid[3][2] = 99;
C grid[2][0] = 99;
D grid[0][3] = 99;
C. Row first, column second: 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).
Tier 3 · AP Mastery

Mastery: 2D Array Creation and Access

MCQ 6
What is printed?
int[][] arr = {
    {1,  2,  3,  4},
    {5,  6,  7,  8},
    {9, 10, 11, 12}
};
System.out.println(arr[2][1] + arr[0][3]);
Locate both elements before adding.
A 13
B 15
C 16
D 14
D. 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.
MCQ 7
A method receives a 2D int array. Which expression gives the number of columns?
A arr.length
B arr[0].length
C arr.length[0]
D arr[1].length + 1
B. 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.
MCQ 8
What is printed?
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]);
Identify which elements were set and which remain default.
A 2
B 6
C 3
D 5
A. Only the diagonal was set. Column 1 values: board[0][1]=0 (default), board[1][1]=2 (set), board[2][1]=0 (default). Sum = 0+2+0 = 2.
MCQ 9
Which declaration creates a 2D array with 4 rows and 2 columns?
A int[][] arr = new int[2][4];
B int[][] arr = new int[4, 2];
C int[][] arr = new int[4][2];
D int[4][2] arr = new int[][];
C. 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.
MCQ 10
What is printed?
int[][] table = {
    {2, 4, 6},
    {8, 10, 12}
};
table[0][1] = table[1][2] - table[0][0];
System.out.println(table[0][1]);
Evaluate the right side before assigning.
A 4
B 10
C 6
D 8
B. 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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]