2D Arrays
in Java
Master grid and matrix data using two-dimensional arrays: declaration, initialization, traversal, and real-world applications.
What Is a 2D Array?
A 2D array is an array of arrays: a data structure that organizes elements into rows and columns, forming a grid or matrix. Each element is accessed using two indices: one for the row, one for the column.
Why Use 2D Arrays?
Any data that is naturally organized in a grid pattern is a perfect candidate for a 2D array. They let you model real-world tables efficiently.
Under the Hood: Array of Arrays
In Java, a 2D array is not a single contiguous block. It is an array of references, where each reference points to a separate 1D array (row). This means rows can even have different lengths (jagged arrays).
Declaring a 2D Array
There are three main ways to create a 2D array in Java:
Accessing Elements
Use two indices: arr[row][col]. The row index selects which inner array, and the column index selects the element within that array.
Traversing with Nested Loops
To visit every element, use a nested for loop: the outer loop iterates over rows, the inner loop iterates over columns within each row.
Row-Major vs Column-Major
The order you traverse matters for performance and for certain algorithms. Java 2D arrays are stored in row-major order.
Enhanced For-Each Loop
Java's enhanced for loop works beautifully with 2D arrays. The outer loop yields each row (a 1D array), and the inner loop yields each element in that row.
How to Apply 2D Arrays
new type[rows][cols] or literal { {...}, {...} } syntax.arr.length for rows and arr[i].length for columns in that specific row.Example 1: Sum All Elements
Given a \( 2 \times 3 \) array, compute the sum of every element.
Example 2: Max per Row
Find the largest value in each row of a 2D array.
Example 3: Tic-Tac-Toe Board
Model a tic-tac-toe board as a \( 3 \times 3 \) char array and check if a row has a winner.
Common Mistakes
arr[col][row] instead of arr[row][col]. Row always comes first in Java.arr.length for columns or arr[0].length for rows. They are not interchangeable.arr[arr.length] causes an ArrayIndexOutOfBoundsException. Valid indices are \( 0 \) to \( \text{length} - 1 \).new int[3][], each row is null until you allocate it. Accessing a null row throws NullPointerException.1D vs 2D Arrays
| 1D Array | 2D Array | |
|---|---|---|
| Declaration | int[] arr |
int[][] arr |
| Indices | arr[i] |
arr[r][c] |
| Size | arr.length |
arr.length (rows), arr[i].length (cols) |
| Traversal | Single loop | Nested loops (2 levels) |
| Use case | Lists, sequences | Grids, matrices, tables |
| Default values | 0 (int), null (Object) | Same per element |
Question 1 of 5
Given int[][] m = new int[4][6];
what does m.length return?
m.length returns the number of rows, which is 4. To get the number of columns, you would use m[0].length, which returns 6.Question 2 of 5
What is the value of arr[1][2] given:int[][] arr = {{5, 10, 15}, {20, 25, 30}};
{20, 25, 30}. Column index 2 selects the third element: 30. Remember, indices start at 0.Question 3 of 5
Which code correctly prints every element of a 2D array grid?
int[] row, and the inner loop iterates over each int val in that row. Option B fails because grid contains int[] references, not plain ints.Question 4 of 5
After executing int[][] j = new int[3][];
what is the value of j[0]?
new int[3][], each row reference defaults to null. You must allocate each row individually (e.g., j[0] = new int[5];) before accessing elements.Question 5 of 5
What is the total number of elements in an array declared as int[][] a = new int[5][3];?