Skip to main content

Unit 4 · Topic 4.13

4.13 Implementing 2D Array Algorithms

The standard array algorithms work on 2D arrays too, either over the whole grid or over one row, one column or some other section. This topic covers totals by row and column, checking neighbors safely, and shifting or reversing a row or column, the building blocks of free-response Question 4.

Key terms

  • row and column totals
  • subsection
  • neighbors
  • 2D min and max

Whole grid, one row, one column

Every 1D algorithm from 4.5 (min and max, sum and average, at least one, all, count, consecutive pairs, duplicates, shift, rotate and reverse) has a 2D version. Decide first which part of the grid the problem is about.

  • The whole grid: nested loops over every row and column.
  • One row r: a single loop over the columns, using grid[r][c]. Or take grid[r] as a 1D array and use the 1D algorithm directly.
  • One column c: a single loop over the rows, using grid[r][c] with c fixed.
  • A section, like the top half or a 3-by-3 block: nested loops with narrower bounds.

Using a row as a 1D array

Because each row is a 1D array, you can reuse 1D code without changing it. This finds the largest value in row 2:

int[] row = grid[2]; int best = row[0]; for (int v : row) { if (v > best) { best = v; } }

Columns don't have this shortcut. For a column, write the loop over the rows yourself.

Totals for each row or column

To total each row, reset the sum to 0 at the start of each outer pass, and store or use it at the end of that pass. For column totals, either swap the loops so the column is on the outside, or keep an array with one running total per column.

Checking neighbors safely

Many grid problems compare a cell with the cells above, below, left and right. Cells on the edges are missing some neighbors, so check the index is in range before you look: r > 0 && grid[r - 1][c] ... for the cell above. Short-circuiting (2.5) makes the bounds check protect the array access, but only if the check comes first.

Reversing a column

Reversing or shifting a row or column is the 1D algorithm with one index held fixed. This swaps the top and bottom of column 1, working toward the middle:

int c = 1; for (int r = 0; r < grid.length / 2; r++) { int temp = grid[r][c]; grid[r][c] = grid[grid.length - 1 - r][c]; grid[grid.length - 1 - r][c] = temp; }

With { {1, 2}, {3, 4}, {5, 6} }, column 1 goes from 2, 4, 6 to 6, 4, 2.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Write a method: column totals

    Write a method columnTotals that returns an array with the sum of each column of grid. One solution:public static int[] columnTotals(int[][] grid) { int[] totals = new int[grid[0].length]; for (int c = 0; c < grid[0].length; c++) { for (int r = 0; r < grid.length; r++) { totals[c] += grid[r][c]; } } return totals; }

    Show the solution
    1. Step 1: The result needs one total per column, so its length is grid[0].length. A new int array starts full of 0s, which is exactly where totals should start.
    2. Step 2: The outer loop picks a column; the inner loop moves down the rows and adds each element to that column's total.
    3. Step 3: For { {3, 1, 4}, {1, 5, 9}, {2, 6, 5} }: column 0 is 3 + 1 + 2 = 6, column 1 is 1 + 5 + 6 = 12, column 2 is 4 + 9 + 5 = 18.

    Answer: The method above; for that grid it returns {6, 12, 18}.

  2. Example 2

    Write a method: counting peaks

    A cell is a peak if it's higher than every neighbor that exists above, below, left and right. Write countPeaks to count the peaks in land. One solution:public static int countPeaks(int[][] land) { int count = 0; for (int r = 0; r < land.length; r++) { for (int c = 0; c < land[0].length; c++) { int h = land[r][c]; boolean peak = true; if (r > 0 && land[r - 1][c] >= h) { peak = false; } if (r < land.length - 1 && land[r + 1][c] >= h) { peak = false; } if (c > 0 && land[r][c - 1] >= h) { peak = false; } if (c < land[0].length - 1 && land[r][c + 1] >= h) { peak = false; } if (peak) { count++; } } } return count; }

    Show the solution
    1. Step 1: Visit every cell with nested loops, and assume it's a peak until a neighbor proves otherwise.
    2. Step 2: Each neighbor check starts with a bounds test, like r > 0 before looking at row r - 1, so edge cells never go out of bounds.
    3. Step 3: Trace { {5, 2, 3}, {1, 4, 8}, {2, 6, 3} }. The 5 at the top-left beats its neighbors 2 and 1. The 8 beats 3, 4 and 3. The 6 beats 4, 2 and 3. Every other cell has a neighbor at least as high.
    4. Step 4: That's 3 peaks.

    Answer: The method above; it returns 3 for that grid.

Common mistakes

  • Forgetting to reset a row total to 0 at the start of each row, so totals pile up across rows.
  • Checking a neighbor before checking that its index is in range.
  • Mixing up grid.length and grid[0].length in the bounds of a column loop.

On the exam

  • Free-response Question 4 (2D Array) is worth 6 points since the 2025 course update, down from 9. Typical tasks are counting or totaling over part of the grid, finding a best element, checking neighbors, or building a new 1D or 2D array from the grid.

Connected topics

Videos

  • AP Computer Science A - Topic 4.13 - Part 1: 2D Array Algorithms

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA Data Collections – Implementing 2D Array Algorithms

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • 2D Array Algorithms in Java | AP CSA Unit 8

    Stefan WebsterWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.13 - Part 2: 2D Array Algorithms

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.13 - Part 3: 2D Array Algorithms

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • 2026 AP Computer Science A Exam Review - Exploring FRQ 4: 2D Arrays

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 4.13 Implementing 2D Array Algorithms. Pick an answer to see if you got it, and why.

Code
int[][] g = {{3, 8, 1}, {6, 2, 10}, {4, 7, 5}};
Question 1 of 4

What is printed as a result of executing the following code segment?int best = 0; int bestSum = 0; for (int r = 0; r < g.length; r++) { int sum = 0; for (int c = 0; c < g[r].length; c++) { sum += g[r][c]; } if (sum > bestSum) { bestSum = sum; best = r; } } System.out.println(best + " " + bestSum);

Question 2 of 4

Which of the following code segments stores in total the sum of the values in column 1 of g (8, 2 and 7)?

Question 3 of 4

What is printed as a result of executing the following code segment?int count = 0; for (int r = 0; r < g.length; r++) { for (int c = 0; c < g[r].length - 1; c++) { if (g[r][c] > g[r][c + 1]) { count++; } } } System.out.println(count);

Question 4 of 4

What is printed as a result of executing the following code segment?int big = g[0][0]; for (int r = 0; r < 2; r++) { for (int c = 1; c < 3; c++) { if (g[r][c] > big) { big = g[r][c]; } } } System.out.println(big);

0 of 4 answered