2D Array Patterns Cheat Sheet for AP CSA

Bookmark This Page

2D Array Patterns Cheat Sheet

Every 2D array template you need for FRQ 4 and MCQ trace questions. Print this. Review it the morning of the exam.

AP CSA Exam: May 15, 2026. FRQ 4 is always 2D arrays. These templates earn you 7–9 points.

Dimensions Quick Reference

What You Need Code
Number of rows grid.length
Number of columns grid[0].length
Access element at row r, col c grid[r][c]
Last row index grid.length - 1
Last col index grid[0].length - 1

Row-Major Traversal (Default)

for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[0].length; c++)
    {
        // process grid[r][c]
    }
}
Use for: visiting every cell, accumulating a total, counting matches, printing all values. This is the default traversal for almost every problem.

Column-Major Traversal

for (int c = 0; c < grid[0].length; c++)
{
    for (int r = 0; r < grid.length; r++)
    {
        // process grid[r][c]
    }
}
Use for: column sums, visiting cells top-to-bottom within each column, or when the problem specifies column-first order.

Row Sum Pattern

for (int r = 0; r < grid.length; r++)
{
    int rowSum = 0;
    for (int c = 0; c < grid[0].length; c++)
    {
        rowSum += grid[r][c];
    }
    System.out.println("Row " + r + " sum: " + rowSum);
}

Column Sum Pattern

for (int c = 0; c < grid[0].length; c++)
{
    int colSum = 0;
    for (int r = 0; r < grid.length; r++)
    {
        colSum += grid[r][c];
    }
    System.out.println("Col " + c + " sum: " + colSum);
}

Find Max in Entire Grid

int max = grid[0][0];
for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[0].length; c++)
    {
        if (grid[r][c] > max)
        {
            max = grid[r][c];
        }
    }
}

Count Matches

int count = 0;
for (int r = 0; r < grid.length; r++)
{
    for (int c = 0; c < grid[0].length; c++)
    {
        if (grid[r][c] == target)
        {
            count++;
        }
    }
}

Main Diagonal (Square Grid Only)

// Only works when grid.length == grid[0].length
for (int i = 0; i < grid.length; i++)
{
    // Main diagonal: grid[i][i]
    // Anti-diagonal: grid[i][grid.length - 1 - i]
}

Check Neighbors (With Bounds)

// Check all 4 neighbors of grid[r][c]
if (r > 0) { /* grid[r-1][c] = above */ }
if (r < grid.length - 1) { /* grid[r+1][c] = below */ }
if (c > 0) { /* grid[r][c-1] = left */ }
if (c < grid[0].length - 1) { /* grid[r][c+1] = right */ }

Get the Full Quick Reference PDF

All 4 units on one printable 12-page guide. Arrays, ArrayList, 2D arrays, and everything else.

Quick Reference Guide Book Tutoring

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]