Unit 4 Cycle 1 Day 21: 2D Array Creation and Access

Unit 4 Foundation (Cycle 1) Day 21 of 28 Foundation

2D Array Creation and Access

Section 4.9 — 2D Arrays

Key Concept

A 2D array in Java is an array of arrays. Declaration: int[][] grid = new int[3][4] creates 3 rows and 4 columns. Access uses two indices: grid[row][col]. The number of rows is grid.length and the number of columns in row r is grid[r].length. All elements are initialized to the type's default value (0 for int, null for objects). The AP exam frequently tests whether you correctly use row-first, column-second indexing.

Consider the following code segment.

int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; System.out.println(grid[2][0]);

What is printed?

Answer: (B) 7

grid[2] is the third row: {7, 8, 9}. grid[2][0] is the first element of that row: 7.

Why Not the Others?

(A) grid[0][2] = 3. The first index is row, second is column.

(C) grid[1][0] = 4. Row 1, column 0.

(D) grid[0][0] = 1. Row 0, column 0.

Common Mistake

2D arrays use [row][col] indexing. The first index selects the row, the second selects the column within that row.

AP Exam Tip

grid[r][c]: r = row, c = column. grid.length = number of rows. grid[0].length = number of columns.

Review this topic: Section 4.9 — 2D Arrays • Unit 4 Study Guide
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.