Row index in AP Computer Science A

In AP Computer Science A, the row index is the first index in the [row][col] notation for a 2D array. It selects which inner array (row) you're working with, runs from 0 to arr.length - 1, and is always written before the column index in Java.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is the row index?

The row index is the first of the two numbers you write when accessing an element of a 2D array in Java, as in arr[row][col]. Per EK 4.11.A.1, a 2D array is really an array of arrays, so the row index does exactly what a 1D array index does. It picks one element out of the outer array. That element just happens to be another array (the row).

This 'array of arrays' idea explains the two length expressions you see constantly on the exam. arr.length is the number of rows, because it's the length of the outer array. arr[r].length is the number of columns in row r, because once the row index r has selected an inner array, you're just asking for that array's length. Like every Java index, the row index starts at 0, so a 5-row array has valid row indices 0 through 4. Go outside that range and you get an ArrayIndexOutOfBoundsException.

Why the row index matters in AP® Computer Science A

The row index lives in Topic 4.11 (2D Arrays) in Unit 4: Data Collections, supporting learning objective 4.11.A, which asks you to develop code that represents collections of related data using 2D arrays. Almost every 2D array skill flows through it. Row-major traversal means the row index is the outer loop variable. Diagonal access means row index equals column index. Summing a column means the row index changes while the column index stays fixed. The College Board has made FRQ Question 4 a 2D array question year after year (2019, 2021, 2024, 2025), and the very first thing those questions test is whether you know which index is which. Swap row and column and your entire solution traverses the grid sideways.

How the row index connects across the course

Column Index (Unit 4)

The row index and column index are a package deal. Row comes first, column comes second, always. A handy mental model is that the row index moves you up and down the grid while the column index moves you left and right across a single row.

1D Array Indexing (Unit 4)

Because a 2D array is stored as an array of arrays (EK 4.11.A.1), the row index is literally just a 1D array index applied to the outer array. arr[r] is a normal 1D array. Everything you know about 1D indexing, including zero-based counting and out-of-bounds errors, transfers directly.

Nested Loops and Row-Major Traversal (Unit 4)

Standard 2D traversal puts the row index in the outer loop (for (int r = 0; r < arr.length; r++)) and the column index in the inner loop. This visits the grid one full row at a time, which is the row-major order the CED expects you to read and write fluently.

Latin Square (Unit 4)

Grid-puzzle problems like Latin squares are built on coordinated row and column access. Checking whether every row contains each value exactly once means fixing the row index and sweeping the column index, then doing the reverse for columns. The 2025 FRQ used exactly this kind of number-puzzle setup.

Is the row index on the AP® Computer Science A exam?

Multiple-choice questions love to test whether you can trace what a row index is doing. A classic stem fixes the column index and loops the row index, like table[r][index] inside a loop over r, then asks what the method computes (answer: a column sum). Another favorite uses arr[i][i], where the same variable serves as both row and column index, to traverse the main diagonal. You'll also see conditions like r + c == 2 that select an anti-diagonal of cells. On the free-response side, Question 4 has been a 2D array question on the 2019, 2021, 2024, and 2025 exams. You're expected to write nested loops with correct bounds (arr.length for rows, arr[r].length for columns), access elements with the row index first, and avoid the classic point-losing mistake of writing arr[col][row].

The row index vs column index

The row index is the FIRST index in arr[row][col]; the column index is the SECOND. Swapping them is the single most common 2D array error on the exam. If it helps, remember that Java's 2D arrays follow 'RC' order, like RC cola: Row, then Column. Also note that arr.length counts rows (valid row indices), while arr[r].length counts columns in row r (valid column indices).

Key things to remember about the row index

  • The row index is the first index in arr[row][col] and selects which inner array (row) of the 2D array you're accessing.

  • Because a 2D array is an array of arrays, arr.length gives the number of rows, so valid row indices run from 0 to arr.length - 1.

  • Once the row index picks a row, arr[r].length tells you how many columns that row has.

  • In standard row-major traversal, the row index is the outer loop variable and the column index is the inner loop variable.

  • When the row index equals the column index, as in arr[i][i], you're on the main diagonal of the array.

  • Writing arr[col][row] instead of arr[row][col] flips your traversal and is one of the most common ways to lose FRQ points on Question 4.

Frequently asked questions about the row index

What is a row index in a 2D array in Java?

The row index is the first index in the arr[row][col] notation. It selects which row (inner array) of the 2D array you want, and like all Java indices it starts at 0.

Does the row index come first or second in Java 2D arrays?

First, always. Java uses arr[row][col] order, so arr[2][5] means row 2, column 5. AP CSA only tests rectangular 2D arrays accessed in this row-then-column order.

Is arr.length the number of rows or columns?

Rows. Since a 2D array is an array of arrays, arr.length is the length of the outer array, which is the row count. Use arr[r].length to get the number of columns in row r.

How is the row index different from the column index?

The row index (first) moves you vertically between rows; the column index (second) moves you horizontally within a row. Mixing them up makes code like a column-sum loop accidentally sum a row instead, which MCQs specifically test.

Can a row index be negative or equal to arr.length?

No. Valid row indices run from 0 to arr.length - 1. Using a negative index or one equal to arr.length throws an ArrayIndexOutOfBoundsException at runtime.