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.
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
Why Use 2D Arrays?
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:
Creating with new
Use the new keyword to allocate a 2D array with a specific number of rows and columns:
Initializer Lists
You can declare and populate a 2D array in one statement using an initializer list:
Accessing Elements
Use two indices in separate brackets: the row index first, then the column index.
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
Modifying Elements
Assign a new value to any cell using the same bracket notation on the left side of an assignment:
Step-by-Step Approach
Example 1: Basic Access
Given the following 2D array, determine the output:
Example 2: Using .length
What does this code print?
Example 3: Last Element
How do you access the last element in a 2D array without hardcoding indices?
Common Mistakes
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]}\)?