6.2 Row-Major Indexing
Row and Column Order in Java 2D Arrays
Unit 6: 2D Arrays and Tile Maps · Lesson 6.2 · about 30 minutes · no coding experience needed
This is the one thing in this unit that must not go wrong, and it goes wrong constantly, because everything you have done so far has been x first and this is the opposite.
What you will be able to do
- Read and write a single cell with the correct index order
- Explain why row comes first
- Recognize a transposed grid and say why a square test map hides it
Words you will need
- Row-major
- In plain words Row first, then column.
- More precisely Indexing where the first subscript selects the row.
- Transposed
- In plain words Flipped along the diagonal. Rows became columns.
- More precisely The result of swapping the two indexes throughout.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A grid with row and column numbers labeled on the edges, and one cell highlighted at row 2 column 1.
int[][] map = { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 9, 0, 0, 0} }; map[2][1] // 9. Row 2, column 1.ROW first, then COLUMN. Always.
`map[2][1]` means: go to row 2 (the third row down), then along to column 1 (the second across). That is where the 9 is.
Read it aloud as "row two, column one". Saying it that way every time is the single most effective way to stop getting it backwards.
-
Step 2
On your screen A diagram contrasting x-then-y screen coordinates with row-then-column array indexing.
setLocation(x, y); // x FIRST, across then down map[row][col]; // row FIRST, down then acrossAnd here is why everybody gets this wrong.
Every coordinate in this course so far has been x first: setLocation, addObject, super. Across, then down.
A 2D array is the OPPOSITE. Row first, which is the down one.
These two orders sit next to each other in the same line of code in 6.4, so being clear now saves a great deal of confusion later.
-
Step 3
On your screen A non-square grid rendered correctly beside the same grid rendered transposed, with the difference obvious.
// A 2 by 4 map. NOT square, on purpose. int[][] m = { {1, 2, 3, 4}, {5, 6, 7, 8} }; m[1][3]; // 8, correct m[3][1]; // THROWS: there is no row 3This map is deliberately not square, and that is the lesson.
Swapping the indexes here throws immediately, because there is no row 3. That is the kind way to find out.
On a SQUARE map, swapping them does not throw. It quietly reads the wrong cell and your map renders transposed, flipped along the diagonal. If your test map is square, the bug is invisible.
Always test with a non-square map. Always.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea `grid[x][y]` is the natural order, like a coordinate.
What is actually true It is `grid[row][col]`, and row is the vertical one.
Why it matters The most common wrong mental model out of any tile-map unit, and every other coordinate in this course reinforces the wrong instinct.
Common wrong idea If the map looks wrong I will notice immediately.
What is actually true On a square map, transposed can look plausible. Test with a non-square map.
Why it matters This is why the bug survives to the project stage.
Common wrong idea Row is the horizontal one.
What is actually true A row is a horizontal LINE, but the row NUMBER counts downward.
Why it matters A genuinely confusing bit of English, and worth stating plainly.
Common wrong idea Swapping the indexes is always caught by an exception.
What is actually true Only when the grid is not square.
Why it matters A student whose test map is 5 by 5 gets no warning at all.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. In `grid[a][b]`, what is a?
-
2. Given {{1,2,3},{4,5,6}}, what is `m[1][2]`?
-
3. Given the same array, what does `m[2][1]` do?
-
4. Why should a test map NOT be square?
-
5. How does `map[row][col]` compare to `setLocation(x, y)`?
Fill in the code
Read the cell on the third row down, second column across, from a map called level. Remember which index comes first and that both count from zero.
int cell = level[][];
Stuck? Open a hint for each blank
- Blank 1: Third row down, counting from zero. And the row index comes first.
- Blank 2: Second column across, counting from zero.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. Java 2D arrays are indexed:
-
2. A tile map renders flipped along the diagonal. The likely cause is:
-
3. On a 5 by 5 map, swapping the indexes:
-
4. The best habit for getting the order right is:
The short version
- `grid[row][col]`. Row FIRST, always.
- That is the opposite order from setLocation(x, y).
- Swapping them transposes the grid.
- A square test map hides the bug completely. Test non-square.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]