The 6 Mistakes
Mistake #1
Swapping Row and Column Bounds
This is the #1 2D array error. grid.length is the number of rows. grid[0].length is the number of columns. Swap them and your loop crashes on non-square grids.
int[][] grid = new int[3][5]; // 3 rows, 5 cols
for (int r = 0; r < grid[0].length; r++) // 5, but only 3 rows
{
for (int c = 0; c < grid.length; c++) // 3, but 5 cols
{
System.out.print(grid[r][c] + " ");
// crashes when r = 3
}
}
CORRECT
for (int r = 0; r < grid.length; r++) // rows
{
for (int c = 0; c < grid[0].length; c++) // cols
{
System.out.print(grid[r][c] + " ");
}
}
grid.length. Inner = cols = grid[0].length. Do this at the start of every 2D array problem.Mistake #2
Accessing [col][row] Instead of [row][col]
In Java 2D arrays, the first index is always the row and the second is always the column. Writing grid[c][r] reads the wrong cell.
// grid has 3 rows, 5 columns // To access row 1, column 3: int val = grid[1][3]; // CORRECT int val = grid[3][1]; // WRONG — accesses row 3 (may be out of bounds)
grid[r][c] — always row first.Mistake #3
Assuming the Grid Is Square
Students assume grid.length == grid[0].length and use a single variable for both bounds. This works for square grids but crashes for rectangular ones.
int size = grid.length;
for (int r = 0; r < size; r++)
{
for (int c = 0; c < size; c++) // wrong if grid is 3x5
{
// ...
}
}
grid.length and grid[0].length separately. Never assume they are equal unless the problem explicitly states a square grid.Crush FRQ 4 on Exam Day
2D arrays are worth 9 points on FRQ 4. Get a structured study plan that covers every pattern.
Get the Cram Kit — $29.99 1-on-1 TutoringMistake #4
Off-by-One in Neighbor Access
Accessing grid[r-1][c] or grid[r][c+1] without checking bounds first. The edges and corners have fewer neighbors.
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[0].length; c++)
{
int above = grid[r - 1][c]; // crashes when r = 0
}
}
CORRECT: Check bounds first
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[0].length; c++)
{
if (r > 0)
{
int above = grid[r - 1][c]; // safe
}
}
}
Mistake #5
Using Enhanced For Loop and Losing Indices
The enhanced for loop on a 2D array gives you each row as a 1D array. You lose access to the row index, which many FRQ problems require.
// Enhanced for on 2D array:
for (int[] row : grid)
{
for (int val : row)
{
// You have the value but NOT the row or column index
// Cannot do grid[r][c] = something
}
}
Mistake #6
Column-Major Traversal When Row-Major Is Expected
Row-major (outer loop = rows) visits all columns in row 0, then all columns in row 1, etc. Column-major (outer loop = columns) visits all rows in column 0 first. The exam specifies which order to use — using the wrong one produces different output.
COLUMN-MAJOR (outer = cols)for (int c = 0; c < grid[0].length; c++)
{
for (int r = 0; r < grid.length; r++)
{
System.out.print(grid[r][c] + " ");
}
}
// Visits: (0,0) (1,0) (2,0) (0,1) (1,1) ...
ROW-MAJOR (outer = rows) — default
for (int r = 0; r < grid.length; r++)
{
for (int c = 0; c < grid[0].length; c++)
{
System.out.print(grid[r][c] + " ");
}
}
// Visits: (0,0) (0,1) (0,2) (1,0) (1,1) ...
Master 2D Arrays Before May 15
54.5% of my AP CSA students score 5s. Get expert help on the hardest topics.
4-Week Cram Kit — $29.99 Book Tutoring