2D arrays: grids of values addressed by row and column.
When data is naturally rectangular — a chessboard, a spreadsheet, a pixel grid — a 2D array is the right structure.
A 2D array is an array whose elements are themselves arrays. You reach an element with two indices: grid[row][col]. Most AP CSA problems treat 2D arrays as rectangular grids, where every row has the same length.
Two nested loops are the standard tool for traversing a 2D array. The outer loop walks rows; the inner loop walks columns. This pattern appears in nearly every 2D array FRQ.
On the AP exam, 2D arrays show up in MCQ tracing, in FRQ #4 (which is often a grid problem), and in any task involving rectangular data such as game boards, image processing, or tabular calculations.
Rows, columns, and nested loops.
A 2D array is an array of arrays — and the loop pattern reflects that.
Declaring and creating
int[][] grid = new int[3][4]; // 3 rows, 4 columns, all 0
String[][] board = new String[8][8]; // 8x8, all null
int[][] m = {{1, 2, 3},
{4, 5, 6}}; // 2 rows, 3 columns
Accessing elements
int x = m[0][2]; // row 0, column 2 -> 3
m[1][0] = 99; // row 1, column 0 -> now 99
The first index is the row; the second is the column. Always.
Dimensions
int rows = m.length; // 2
int cols = m[0].length; // 3
m.length gives the number of rows. m[r].length gives the number of columns in row r. For rectangular arrays this is the same for every row.
Row-major traversal (the standard)
for (int r = 0; r < m.length; r++) {
for (int c = 0; c < m[r].length; c++) {
System.out.print(m[r][c] + " ");
}
System.out.println();
}
This visits row 0 fully, then row 1, and so on. The println after the inner loop creates the row-by-row layout.
Column-major traversal
for (int c = 0; c < m[0].length; c++) {
for (int r = 0; r < m.length; r++) {
// process m[r][c]
}
}
Swap the loops to walk all of column 0 first, then column 1, and so on.
Enhanced for over a 2D array
for (int[] row : m) {
for (int n : row) {
System.out.print(n + " ");
}
}
The outer loop pulls out each row (an int[]); the inner loop walks that row's values. Cannot modify the cells through this form.
2D array terminology.
Used in nearly every grid problem on the AP exam.
- 2D array — an array of arrays, addressed by two indices.
- Row — the first index;
grid[r]is one whole row. - Column — the second index; same column number across rows.
- Rectangular array — every row has the same length.
- Row-major traversal — outer loop = rows, inner loop = columns.
- Column-major traversal — outer loop = columns, inner loop = rows.
- m.length — number of rows.
- m[r].length — number of columns in row
r.
How 2D arrays are tested.
Index order, dimensions, and traversal direction dominate.
MCQ patterns to expect:
- Row vs column index. The first index is always the row.
- Wrong dimension bound. The inner loop should use
m[r].length, notm.length. - Row-major vs column-major output. Questions ask which traversal produces a given output.
- Modification via enhanced for. Cannot change cells through a nested enhanced-for loop.
FRQ tip: when a problem says "rows" and "columns", use variable names like r and c to keep the code self-documenting.
Sum the elements of a 2D array.
A textbook nested-loop traversal.
Problem. Write a method that returns the sum of all elements in a 2D int array.
Step 1. Use row-major traversal. Outer loop walks rows; inner walks columns.
Step 2. Use arr.length for the outer bound and arr[r].length for the inner bound. This works even if rows have different lengths.
Step 3. Accumulate into a single int.
public static int sum(int[][] arr) {
int total = 0;
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[r].length; c++) {
total += arr[r][c];
}
}
return total;
}
Trace.
For {{1, 2, 3}, {4, 5, 6}}:
r=0 adds 1+2+3 = 6; r=1 adds 4+5+6 = 15. Total = 21.
Enhanced-for alternative:
public static int sum(int[][] arr) {
int total = 0;
for (int[] row : arr) {
for (int n : row) {
total += n;
}
}
return total;
}
Why this matters. Sum, max, count, and "matches" all share this exact nested template. Memorize it and you're set for most 2D MCQs and FRQs.
2D array errors that crash code.
Most come from confusing rows with columns.
- Swapping row and column indices.
grid[c][r]instead ofgrid[r][c]may throw an exception or read the wrong cell. - Wrong inner bound. Using
m.lengthfor both loops crashes when the grid is not square. - Reusing the same loop variable for both loops. Use different names like
randc. - Mutating via enhanced for. The cell variable is a copy; assigning to it does nothing.
- Missing
println()after the inner loop — all rows collapse onto one line. - Treating a 2D array as 1D.
m[0]is a full row, not a single element.
2D array essentials.
Pin this in memory for every grid problem.
2D Arrays Reference
AP Quick Reference| Concept | Meaning | AP Exam Tip |
|---|---|---|
grid[r][c] |
Element at row r, column c |
Row comes first, always. |
grid.length |
Number of rows | Use as the outer loop bound. |
grid[r].length |
Number of columns in row r |
Use as the inner loop bound. |
| Row-major traversal | Outer = rows, inner = columns | Default style on the AP exam. |
| Column-major traversal | Outer = columns, inner = rows | Swap loops to change direction. |
| Enhanced for | Outer pulls a row, inner pulls a cell | Read-only over cells. |
How to lock in 2D array fluency.
Treat the row-major template as a reflex.
Use the three practice tools below this lesson in order:
- MCQ Practice — draw the grid with row and column labels. Mark each cell as the trace visits it. This wipes out row/column confusion.
- Java Lab — write sum, max, count-matches, and find-row-max methods for 2D arrays. Always use
arr.lengthandarr[r].length. - FRQ Practice — FRQ #4 is often a 2D grid problem. Begin every solution by writing the row-major template, then fill in the body.
If you can write the row-major template in 10 seconds, 2D FRQs become predictable points.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.