4m left·0%
Reading Time: 4 min
Last Updated: March 23, 2026
Main Ideas: 4
Reading Time: 4 min
Last Updated: March 23, 2026
Main Ideas: 4

Topic 4.11 Notes – 2D Array Creation and Access

Verified for 2027 AP® Computer Science A Exam
Read aloud
A 2D array is used to store data in rows and columns, like a grid or table. You’ll learn how they’re built, how indexing works, and how to correctly use .length so you don’t crash your program with an index error.

1. What a 2D Array Is

A 2D array in Java is an array of arrays.

That means:

  • The main array holds rows.
  • Each row is itself a 1D array.
  • Each element inside that row is a value.

If we write:

int[][] grid = new int[3][4];
  • grid.length → number of rows → 3
  • grid[0].length → number of columns in row 0 → 4

On the AP exam, 2D arrays are always rectangular, meaning:

  • Every row has the same number of columns.
  • No jagged arrays are tested.

The size is fixed when created. You cannot resize it later.

2D arrays can store:

  • Primitives like int, double, boolean
  • Object references like String or custom class objects

2. Creating and Initializing 2D Arrays

Using new with Dimensions

int[][] board = new int[2][3];

This creates:

  • 2 rows
  • 3 columns per row

Every element is automatically set to a default value:

TypeDefault Value
int0
double0.0
booleanfalse
reference typesnull

Students sometimes forget this and think the array is “empty.” It isn’t. It’s filled with defaults.

Using an Initializer List

int[][] scores = {
    {7, 8, 9},
    {10, 11, 12}
};

Each inner {} represents a row.

  • Number of rows = number of inner lists
  • Number of columns = number of elements inside each row

This is cleaner when you already know the values.

3. Accessing Elements and Row-Major Order

Access format:

arr[row][col]

The AP exam follows row-major order:

  • First index → row
  • Second index → column

If you want the element in row 1, column 2:

arr[1][2]

Say it in your head: row first, column second.
Most mistakes happen because students flip them.

Accessing an Entire Row

arr[1]

This gives you the whole second row as a 1D array.

That’s why:

arr[row].length

works. You’re asking, “How long is this row array?”

Using .length Correctly

This is where MC questions love to trap people.

  • arr.length → number of rows
  • arr[row].length → number of columns in that row

Valid indexes:

  • Rows → 0 through arr.length - 1
  • Columns → 0 through arr[row].length - 1

If you go outside those ranges, you get:

ArrayIndexOutOfBoundsException

Classic mistake inside nested loops:

for (int col = 0; col < arr.length; col++)  // WRONG

Correct version:

for (int col = 0; col < arr[row].length; col++)

Rows use arr.length.
Columns use arr[row].length.

4. Putting It All Together

The standard traversal pattern looks like this:

for (int row = 0; row < arr.length; row++) {
    for (int col = 0; col < arr[row].length; col++) {
        // process arr[row][col]
    }
}

Outer loop → rows
Inner loop → columns

The inner loop finishes completely before the row changes. That detail matters when tracing code on quizzes.

On FRQ 4, you almost always:

  • Count values
  • Sum values
  • Modify elements
  • Search for something specific

Every one of those uses this same nested structure.

Key Takeaways

A 2D array is an array of arrays, so arr[row] gives you a 1D array.
The AP exam assumes 2D arrays are rectangular.
arr.length is rows; arr[row].length is columns.
Index order is always row first, column second.
Loop bounds must use <, never <=.
Most 2D array errors come from mixing up .length or flipping row and column.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining