01 / 22

2D Array
Creation & Access

Master the fundamentals of two-dimensional arrays in Java: how to declare, initialize, and access elements using row and column indices.

AP Computer Science A

What is a 2D Array?

A 2D array is an array of arrays. Think of it as a table with rows and columns, where each cell stores a value. In Java, a 2D array is really a one-dimensional array whose elements are themselves one-dimensional arrays.

Visualizing the Grid

col 0 col 1 col 2 col 3 row 0 row 1 row 2 5 12 7 3 9 1 8 4 2 6 11 10
\( \text{arr}[1][2] = 8 \)

Why Use 2D Arrays?

Game Boards
Tic-tac-toe, chess, and sudoku grids map naturally to rows and columns.
Spreadsheets
Tabular data like grades across students and assignments.
Images
Pixels are stored in a grid: each cell holds a color value.

Any data that is naturally organized in a grid of rows and columns is a perfect candidate for a 2D array.

Declaring a 2D Array

In Java, you declare a 2D array by specifying the element type followed by two pairs of brackets:

// Declaration only (no object created yet) int[][] grid; String[][] board; double[][] matrix;

Creating with new

Use the new keyword to allocate a 2D array with a specific number of rows and columns:

// 3 rows, 4 columns, all initialized to 0 int[][] grid = new int[3][4];
1
First dimension = number of rows (3)
2
Second dimension = number of columns (4)
3
Default values: \(0\) for int, \(0.0\) for double, \(\texttt{null}\) for objects, \(\texttt{false}\) for boolean

Initializer Lists

You can declare and populate a 2D array in one statement using an initializer list:

int[][] grid = { {5, 12, 7, 3}, // row 0 {9, 1, 8, 4}, // row 1 {2, 6, 11, 10} // row 2 };

Accessing Elements

Use two indices in separate brackets: the row index first, then the column index.

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

1D vs 2D Arrays

1D Array 2D Array
Declaration \(\texttt{int[] arr}\) \(\texttt{int[][] arr}\)
Creation \(\texttt{new int[5]}\) \(\texttt{new int[3][4]}\)
Access \(\texttt{arr[i]}\) \(\texttt{arr[r][c]}\)
Length \(\texttt{arr.length}\) \(\texttt{arr.length}\) (rows) / \(\texttt{arr[0].length}\) (cols)
Structure Single list Array of arrays (grid)

The .length Property

int[][] grid = new int[3][4]; // Number of rows int rows = grid.length; // 3 // Number of columns in row 0 int cols = grid[0].length; // 4 // Total elements int total = rows * cols; // 12
grid
\( \texttt{grid[0]} \)
\( \texttt{grid[1]} \)
\( \texttt{grid[2]} \)
0 0 0 0
\( .\text{length}=4 \)
0 0 0 0 0 0 0 0
3
.length

Modifying Elements

Assign a new value to any cell using the same bracket notation on the left side of an assignment:

int[][] grid = { {5, 12, 7}, {9, 1, 8} }; grid[0][2] = 99; // row 0, col 2: 7 becomes 99 grid[1][0] = 42; // row 1, col 0: 9 becomes 42 System.out.println(grid[0][2]); // 99 System.out.println(grid[1][0]); // 42

Step-by-Step Approach

1
Identify dimensions: How many rows and columns does the problem require?
2
Choose creation method: Use \(\texttt{new}\) for default values, or an initializer list for known data.
3
Access with \[\text{row}\]\[\text{col}\]: Always provide the row index first, column index second.
4
Use .length wisely: \(\texttt{arr.length}\) for row count, \(\texttt{arr[0].length}\) for column count.
5
Check bounds: Valid indices are \(0\) to \(\texttt{length} - 1\) for both dimensions.

Example 1: Basic Access

Given the following 2D array, determine the output:

int[][] m = { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} }; System.out.println(m[2][0]); // ? System.out.println(m[0][2]); // ? System.out.println(m[1][1]); // ?

Example 2: Using .length

What does this code print?

String[][] names = { {"Alice", "Bob"}, {"Carol", "Dave"}, {"Eve", "Frank"} }; System.out.println(names.length); // ? System.out.println(names[0].length); // ? System.out.println(names[2][1]); // ?

Example 3: Last Element

How do you access the last element in a 2D array without hardcoding indices?

int[][] data = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; int lastRow = data.length - 1; // 2 int lastCol = data[lastRow].length - 1; // 3 int lastVal = data[lastRow][lastCol]; // 12

Common Mistakes

✕ Swapping Row and Column
Writing \(\texttt{arr[col][row]}\) instead of \(\texttt{arr[row][col]}\). Java always expects row first.
✕ Using arr[r, c] Syntax
Java uses \(\texttt{arr[r][c]}\) with separate brackets. The comma syntax \(\texttt{arr[r, c]}\) does not work.
✕ Off-by-One on .length
Using \(\texttt{arr.length}\) as an index causes ArrayIndexOutOfBoundsException. The last valid index is \(\texttt{length} - 1\).

Quick Reference Table

Operation Syntax Example
Declare \(\texttt{type[][] name}\) \(\texttt{int[][] grid}\)
Create (empty) \(\texttt{new type[rows][cols]}\) \(\texttt{new int[3][4]}\)
Create (values) \(\{\{...\}, \{...\}, ...\}\) \(\{\{1,2\},\{3,4\}\}\)
Read element \(\texttt{name[r][c]}\) \(\texttt{grid[1][2]}\)
Write element \(\texttt{name[r][c] = val}\) \(\texttt{grid[1][2] = 99}\)
Row count \(\texttt{name.length}\) \(\texttt{grid.length}\)
Column count \(\texttt{name[0].length}\) \(\texttt{grid[0].length}\)

Question 1 of 5

Given \(\texttt{int[][] arr = new int[4][3];}\)
What is the value of \(\texttt{arr.length}\)?

Question 2 of 5

Given:
\(\texttt{int[][] m = \{\{10,20,30\},\{40,50,60\}\};}\)
What does \(\texttt{m[1][0]}\) evaluate to?

Question 3 of 5

Which line of code correctly creates a 2D array with \(5\) rows and \(2\) columns?

Question 4 of 5

Given:
\(\texttt{double[][] vals = new double[3][3];}\)
What is the value of \(\texttt{vals[0][0]}\) after creation?

Question 5 of 5

Given:
\(\texttt{int[][] a = \{\{1,2,3\},\{4,5,6\},\{7,8,9\},\{10,11,12\}\};}\)
What is the value of \(\texttt{a[a.length-1][a[0].length-1]}\)?