Row-Major Order

Row-major order is the standard way to traverse a 2D array on AP Computer Science A, where you move across each entire row before dropping to the next one, which in Java means an outer loop over row indices and an inner loop over column indices.

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

What is Row-Major Order?

Row-major order describes the path you take through a 2D array. You read across the first row left to right, then the second row, then the third, exactly like reading a page of text. In Java, this matches how 2D arrays actually exist, since a 2D array is really an array of arrays where each inner array is one row.

In code, row-major traversal is the classic nested loop with the row index on the outside and the column index on the inside, so you access arr[row][col] as col runs through a full row before row increases. Nested enhanced for loops give you the same row-major behavior automatically. The outer loop hands you one row (a 1D array) at a time, and the inner loop walks through that row's elements in order.

Why Row-Major Order matters in AP Computer Science A

Row-major order lives in the 2D Array unit of AP CSA, where the CED explicitly defines it as traversal that occurs across each row and expects you to trace and write nested loops that visit every element. It's also the unspoken default of the whole exam. When a question says "process the elements of the 2D array" without specifying an order, row-major is the assumed pattern, and free-response 2D array problems almost always expect you to write it from scratch. Mixing up which index is the row and which is the column is one of the most common ways points disappear on that FRQ, so knowing row-major cold is cheap insurance.

How Row-Major Order connects across the course

Column-Major Order (Unit 8)

Column-major is row-major's mirror image. You travel down each column before moving right to the next one, which in Java code just means swapping the nested loops so the column index is on the outside. Same elements visited, different path.

Array (Unit 6)

Row-major order works because a 2D array in Java is literally a 1D array whose elements are other 1D arrays. Each arr[row] is a regular array, so everything you learned about traversing 1D arrays applies to one row at a time.

Indexing (Units 6 & 8)

Row-major traversal depends on getting arr[row][col] right, with the row index always first. The bounds matter too. Rows run from 0 to arr.length - 1, and columns run from 0 to arr[0].length - 1, and flipping those is a classic ArrayIndexOutOfBoundsException.

Iteration (Unit 4)

Row-major order is what nested loops were building toward. The outer loop picks a row, the inner loop sweeps across it, and tracing that pattern by hand is exactly the skill MCQs test when they ask what a nested loop prints.

Is Row-Major Order on the AP Computer Science A exam?

On the multiple-choice section, row-major order shows up in code-tracing questions. You're given a nested loop over a 2D array and asked what gets printed or which elements get changed, and the answer choices are designed to catch people who confuse row-major with column-major or who swap the row and column indices. On the free-response section, Question 4 is traditionally the 2D array question, and writing a correct row-major traversal (outer loop over rows, inner loop over columns, correct bounds using arr.length and arr[0].length) is often the backbone of the solution. The CED also expects you to know that nested enhanced for loops traverse in row-major order, so recognize that for (int[] row : arr) followed by for (int val : row) visits elements in the same order as the index-based version.

Row-Major Order vs Column-Major Order

Row-major goes across each row first (read like a book); column-major goes down each column first. In code, the difference is purely which loop is on the outside. Row-major puts the row index in the outer loop, column-major puts the column index there. Both visit every element, but the order of visits differs, which changes printed output and matters whenever order affects the result. The AP exam treats row-major as the default and uses column-major mainly to test whether you can actually trace the loops instead of pattern-matching.

Key things to remember about Row-Major Order

  • Row-major order means traversing a 2D array across each full row before moving to the next row, like reading lines of text.

  • In Java code, row-major traversal puts the row index in the outer loop and the column index in the inner loop, accessing elements as arr[row][col].

  • Nested enhanced for loops always traverse a 2D array in row-major order because the outer loop hands you one row array at a time.

  • The number of rows is arr.length and the number of columns is arr[0].length, and mixing these up causes out-of-bounds errors on the FRQ.

  • Column-major order is the reverse pattern (down each column first), and swapping the two nested loops converts one traversal into the other.

  • When an AP CSA question doesn't specify a traversal order for a 2D array, assume row-major.

Frequently asked questions about Row-Major Order

What is row-major order in AP Computer Science A?

Row-major order is traversing a 2D array one row at a time, moving across each row from left to right before going to the next row. In Java it's a nested loop with the row index outside and the column index inside, accessing arr[row][col].

What's the difference between row-major and column-major order?

Row-major moves across rows first (like reading a book); column-major moves down columns first. In code, the only change is swapping which loop is nested inside the other. Both visit all elements but in a different sequence.

Do enhanced for loops traverse a 2D array in row-major order?

Yes, always. A nested enhanced for loop like for (int[] row : arr) with an inner for (int val : row) visits elements in row-major order, because the outer loop returns each row array in sequence. You can't get column-major order from enhanced for loops without extra work.

Is row-major order on the AP CSA exam?

Yes. The CED defines it in the 2D array unit, MCQs test it through nested-loop tracing, and the 2D array free-response question typically requires you to write a row-major traversal with correct bounds.

In arr[row][col], which index comes first in Java?

The row index always comes first, so arr[2][5] means row 2, column 5. The row count is arr.length and the column count is arr[0].length, and the AP exam only uses rectangular 2D arrays where every row has the same length.