TOPIC 4.11 · STUDY
2D Array Creation and Access
A rectangular 2D array is an array of equal-length row arrays. Create one with new int[rows][cols] or with an initializer list, then read or write a cell with grid[r][c]. Indexes start at 0; grid.length is the row count and grid[0].length is the column count when every row has the same length.
4.11.A
2D array creation and access
A two-dimensional array stores values in rows and columns. In Java it is an array of 1D arrays: each row is itself an array of cells. For AP CSA this topic focuses on rectangular grids, where every row has the same number of columns.
Create a rectangular int grid with new. The first size is the number of rows; the second is the number of columns in each row. Every new int cell starts at 0 until you assign a different value.
int[][] lockers = new int[2][3];
System.out.print(lockers[0][1]); // 0
An initializer list builds the grid and fills the cells in one step. Each inner brace is one row. Indexes still start at 0: the first row is index 0, and the first cell in that row is column 0.
int[][] seats = { {3, 8, 2}, {5, 1, 4} };
System.out.print(seats[1][0]); // 5
seats[r][c] picks row r then column c. seats[0][1] is 8; seats[1][2] is 4.
For a rectangular grid, seats.length is the number of rows. seats[0].length is the number of columns in row 0, which matches every other row when the grid is rectangular. The element at row r and column c is seats[r][c]. Writing seats[0][1] = 9 replaces only that one cell.
System.out.print(seats.length); // 2 rows
System.out.print(seats[0].length); // 3 columns
seats[0][1] = 9;
System.out.print(seats[0][1]); // 9
A single index selects an entire row array, not one int. int[] front = seats[0]; makes front refer to the same row as seats[0], so front[2] is the same cell as seats[0][2]. Legal row indexes run from 0 through seats.length - 1, and legal column indexes in a rectangular row run from 0 through seats[0].length - 1. An index outside that range throws ArrayIndexOutOfBoundsException.
Default values depend on the element type. new boolean[rows][cols] fills cells with false. new double[rows][cols] fills cells with 0.0. new String[rows][cols] fills cells with null because String is a reference type. The size of the grid is fixed when it is created; you cannot grow it by assigning to length.
Key points
- new int[rows][cols] creates a rectangular grid; each int cell starts at 0.
- An initializer list sets the rows and cell values in one declaration.
- grid[r][c] accesses the element at row r and column c; indexes start at 0.
- grid.length is the number of rows; grid[0].length is the column count for a rectangular grid.
- grid[r] is the whole 1D row array at index r.
- boolean cells default to false, double cells to 0.0, and String cells to null.
- Out-of-range row or column indexes throw ArrayIndexOutOfBoundsException.
grid.length is the row count, not the number of cells. A 2-by-3 int grid has length 2 even though it stores six values; use grid[0].length for the column count, not length * something.