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
// code runner! try it out in vscode 
String[][] seating = {
    {"Ava", "Ben", "Chris"},
    {"Diana", "Ethan", "Faith"},
    {"Grace", "Henry", "Ivy"}
};

//remove these commands from comments one at a time to try them out. 

// seating[0][1]
// seating[2][0]
// seating[1][2]



// they should return "ben", "grace", and "faith" respectively.


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:

// code runner! check if you get the correct output, and see if you can find the output before you run the comamand!

int[][] numbers = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

System.out.println(numbers[2][1]);

Step-by-step:

  1. Find row 2 → {7, 8, 9}
  2. Find column 1
  3. Return the value

Output:

8

Updating Elements

Arrays are mutable, meaning their values can be changed after creation.

Example:

// code runner! can you notice what's changed?
int[][] board = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

board[1][1] = 100;

Can you notice what changed?

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.
// try editing this code for your hack!

String[][] groups = {
    {"Alice", "Bob", "Cara"},
    {"David", "Emma", "Frank"},
    {"Grace", "Henry", "Isabella"}
};

Reflection Questions:

  1. Why is a 2D array better than a 1D array for this situation?
  2. Which index represents the row?
  3. Which index represents the column?
  4. What changes would need to be made if each subgroup had 4 students instead of 3?