4.11 Two-Dimensional Array Creation and Access
Create and access two-dimensional arrays using rows and columns, and identify valid positions in a rectangular grid.
What is a 2D Array?
A 2D array is an array whose elements are themselves arrays.
Think of a 2D array as a table, spreadsheet, seating chart, or game board. Instead of storing data in a single line like a 1D array, a 2D array organizes data into rows and columns.
Example: Student Grades
| Student | Quiz 1 | Quiz 2 | Quiz 3 |
|---|---|---|---|
| Alice | 90 | 85 | 92 |
| Bob | 78 | 88 | 81 |
| Cara | 95 | 91 | 97 |
This information can be stored in Java as:
int[][] grades = {
{90, 85, 92},
{78, 88, 81},
{95, 91, 97}
};
Each row stores information about one student, while each column stores information about one quiz.
A 2D array requires two indices:
grades[row][column]
The first index selects a row.
The second index selects a column.
For example:
grades[1][2]
- Row 1 →
{78, 88, 81} - Column 2 →
81
Result:
81
Why Use 2D Arrays?
A 1D array stores information in a single list:
String[] students = {
"Alice",
"Bob",
"Cara"
};
This works when there is only one category of information.
However, many real-world situations require data to be organized into rows and columns.
Examples include:
- Classroom seating charts
- Monthly calendars
- Game boards
- Student gradebooks
- Tournament brackets
- Pixel locations in an image
A 2D array allows related information to be grouped together in a structured way.
Visualizing Rows and Columns
Consider the following seating chart:
String[][] seating = {
{"Ava", "Ben", "Chris"},
{"Diana", "Ethan", "Faith"},
{"Grace", "Henry", "Ivy"}
};
Column
0 1 2
Row 0 Ava Ben Chris
Row 1 Diana Ethan Faith
Row 2 Grace Henry Ivy
Examples:
seating[0][1]
returns:
"Ben"
seating[2][0]
returns:
"Grace"
seating[1][2]
returns:
"Faith"
Understanding rows and columns is one of the most important skills when working with 2D arrays.
Declaring a 2D Array
Declaring a 2D array is very similar to declaring a 1D array.
1D Array
int[] scores;
2D Array
int[][] scores;
The extra pair of brackets indicates that each element is itself an array.
Examples:
int[][] numbers;
String[][] names;
Student[][] roster;
Initializing a 2D Array
Method 1: Initialize with Values
int[][] grid = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Method 2: Create an Empty Grid
int[][] grid = new int[3][3];
This creates a 3-row by 3-column array.
All integer values start as 0.
0 0 0
0 0 0
0 0 0
Accessing Elements
The general format is:
array[row][column]
Example:
int[][] numbers = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
System.out.println(numbers[2][1]);
Step-by-step:
- Find row 2 →
{7, 8, 9} - Find column 1
- Return the value
Output:
8
Another example:
System.out.println(numbers[0][2]);
Output:
3
Updating Elements
Arrays are mutable, meaning their values can be changed after creation.
Example:
int[][] board = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
board[1][1] = 100;
Before:
1 2 3
4 5 6
7 8 9
After:
1 2 3
4 100 6
7 8 9
Only the specified location changes.
The rest of the array remains unchanged.
2D Arrays with Objects
2D arrays can store objects just like 1D arrays.
Example:
Student[][] classroom = new Student[3][3];
Each location can store a reference to a Student object.
classroom[0][0] = new Student("Alice", 16);
classroom[1][2] = new Student("Bob", 17);
Accessing methods works the same way:
System.out.println(classroom[0][0].getName());
Output:
Alice
This allows complex information to be organized into rows and columns.
HACK: AP CSA 2D Arrays
Part 1: AP CSA Multiple Choice
Question 1
int[][] nums = {
{1, 2, 3},
{4, 5, 6}
};
What is the value of:
nums[1][2]
A. 3
B. 4
C. 5
D. 6
Question 2
String[][] names = {
{"Ava", "Ben"},
{"Cara", "David"}
};
What is printed?
System.out.println(names[0][1]);
A. Ava
B. Ben
C. Cara
D. David
Question 3
Which statement correctly updates the value 7 to 20?
int[][] arr = {
{1,2,3},
{4,5,6},
{7,8,9}
};
A.
arr[2][0] = 20;
B.
arr[0][2] = 20;
C.
arr[2][2] = 20;
D.
arr[1][0] = 20;
Question 4
What does the first index represent in a 2D array?
A. Column
B. Row
C. Value
D. Length
Question 5
Which declaration creates a 2D array of Strings?
A.
String[] names;
B.
String[][] names;
C.
String names[][];
D. Both B and C
Part 2: Representing your Group
Create a 2D array containing the members of your group.
Requirements:
- Each row represents your group of 2 or 3.
- Each row represents a subgroup within your larger group.
- Make sure every member of your group is in the array.
- Add comments explaining what each subgroup represents.
Example:
String[][] groups = {
{"Alice", "Bob", "Cara"},
{"David", "Emma", "Frank"},
{"Grace", "Henry", "Isabella"}
};
Reflection Questions:
- Why is a 2D array better than a 1D array for this situation?
- Which index represents the row?
- Which index represents the column?
- What changes would need to be made if each subgroup had 4 students instead of 3?
Submit Failed — Enter Manually
Submit Assignment
Need to update a submission later? Open the submissions dashboard.