Skip to main content
The new Teacher Workspace is here. Your first 3 assignments are free. Try it →
A colleague sent you $30 off Fiveable. Get the annual plan for $99 →

Row-Major Order in AP Computer Science A

In AP Computer Science A, row-major order is a way of traversing a 2D array that visits every element across row 0, then across row 1, and so on. It is usually written as nested for loops, with the outer loop over rows and the inner loop over columns (EK 4.12.A.1).

Verified for the 2027 AP Computer Science A exam•Last updated October 2026

What is Row-Major Order?

Row-major order is a traversal pattern for 2D arrays. You go across an entire row, left to right, before dropping down to the next row. Think of it as reading a page of a book: finish the line, then move to the next line.

In Java, a 2D array is stored as an array of arrays, so each row is its own 1D array. That makes row-major order the natural way to walk through it. The outer loop picks a row and the inner loop walks across that row's columns:

</>Java
for (int r = 0; r < grid.length; r++) {
    for (int c = 0; c < grid[0].length; c++) {
        System.out.print(grid[r][c] + " ");
    }
}

The CED (EK 4.12.A.1) defines row-major order as "an ordering of 2D array elements where traversal occurs across each row," in contrast to column-major order, which goes down each column. On the AP exam, the term is about the order you visit elements in your code. It is not a question about how memory is laid out.

Why Row-Major Order matters in AP® Computer Science A

Row-major order lives in Topic 4.12 Traversing 2D Arrays in Unit 4: Data Collections. It directly supports learning objective AP Comp Sci A 4.12.A, which asks you to develop code that traverses a 2D array and determine the result of those traversals.

This is the default pattern for almost any 2D array task. Summing a grid, finding a max, counting matches, and checking for duplicates all rely on it. If you can't predict which element a nested loop visits third or tenth, you can't trace output, debug a loop, or write a correct 2D array FRQ method. Row-major order is the baseline, and column-major order and custom orders are variations you build from it.

How Row-Major Order connects across the course

Column-Major Order (Unit 4)

Column-major order is row-major order with the loops flipped. The outer loop walks the columns (bounded by grid[0].length) and the inner loop walks the rows (bounded by grid.length). Same elements, different visiting order, so swapping which loop is outside changes the printed result.

1D Array Traversal (Unit 4)

Because a 2D array is an array of arrays, each row in a row-major traversal is just a 1D array traversal. The inner loop is the same for loop you already wrote for 1D arrays, repeated once per row.

Enhanced For Loop (Unit 4)

Nested enhanced for loops always traverse in row-major order. The outer loop for (int[] row : grid) grabs each row, and the inner loop for (int val : row) walks across it. You can't get column-major order this way, because the outer loop can only hand you rows.

Indexing and containsDuplicates-style Algorithms (Unit 4)

In grid[r][c], the first index is always the row. Row-major order keeps the row fixed while the column index changes fastest. Algorithms like checking a 2D array for duplicates use this pattern to compare every element in a predictable order.

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

Multiple choice. Expect identification questions that describe a path and ask you to name it. For example, a 3-row by 4-column array traversed "left to right across row 0, then left to right across row 1, then across row 2" is row-major order. Moving "down each column from top to bottom before shifting to the next column" is column-major order. Code-tracing questions are also common. They show you nested loops and ask what prints, which element is visited at a certain step, or what value a variable holds afterward (LO 4.12.A).

What you need to do:

  • Look at the outer loop. If it controls the row index, the traversal is row-major.
  • Check the bounds. Rows use arr.length and columns use arr[0].length. Mixing them up on a non-square array causes an ArrayIndexOutOfBoundsException or skips elements.
  • Predict the exact visiting order for a small grid by hand.

Free response. No released FRQ has used the phrase "row-major order" verbatim. Still, 2D array FRQs routinely expect you to write a nested loop that visits every element, and row-major is the standard way to do it.

Row-Major Order vs Column-Major Order

Row-major order goes across each row before moving down. The outer loop controls rows and the column index changes fastest. Column-major order goes down each column before moving right. The outer loop controls columns and the row index changes fastest. Both visit every element exactly once. Only the order differs, and that order changes things like printed output. A quick check is to ask what the outer loop is counting. Rows means row-major, and columns means column-major.

Key things to remember about Row-Major Order

  • Row-major order visits a 2D array across row 0, then across row 1, and continues row by row until the last row.

  • In a row-major nested loop, the outer loop controls the row index and the inner loop controls the column index.

  • Row bounds use arr.length and column bounds use arr[0].length, and swapping them can break the traversal on non-square arrays.

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

  • Column-major order is the same idea with the loops flipped, so it goes down each column instead of across each row.

  • On the AP exam, row-major order describes the order your code visits elements, which ties directly to learning objective 4.12.A.

Frequently asked questions about Row-Major Order

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

Row-major order is a 2D array traversal that goes across each row before moving to the next one. For a 3-by-4 array, you visit all 4 elements of row 0, then all 4 of row 1, then all 4 of row 2.

Does row-major order mean how Java stores a 2D array in memory?

Not on the AP exam. The CED defines row-major order as the order in which a traversal visits elements, going across each row. Java does store 2D arrays as arrays of arrays, which is why row-major traversal feels natural, but exam questions ask about loop order, not memory layout.

How is row-major order different from column-major order?

Row-major goes across each row with the outer loop on rows, while column-major goes down each column with the outer loop on columns. Both reach every element. In a 2-by-3 array, row-major visits [0][0], [0][1], [0][2] first, and column-major visits [0][0], [1][0], [0][1] first.

Can you do column-major order with an enhanced for loop?

No. A nested enhanced for loop always gives you one row at a time, so it can only traverse in row-major order. For column-major order you need regular indexed for loops with the column loop on the outside.

How do I tell if a nested loop is row-major on the AP exam?

Check what the outer loop counts. If it runs from 0 to arr.length and is used as the first index in arr[r][c], it is row-major. If the outer loop runs to arr[0].length and is used as the column index, it is column-major.