☕ 30 Important APCS-A Algorithms & Code Patterns

Quickly review these important code structures for the AP Computer Science A Exam (2026).

Description:
Visiting each element in an array, usually to process or examine values.
for (int i = 0; i < arr.length; i++) {
    // process arr[i]
}

Description:
Iterating over all elements in a collection or array.
for (int value : arr) {
    // process value
}

Description:
Finding the largest or smallest value (or index) in a sequence.
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
        max = arr[i];
    }
}

Description:
Counting elements that meet a specific criterion.
int count = 0;
for (int num : arr) {
    if (num % 2 == 0) {
        count++;
    }
}

Description:
Creating a new collection from selected elements.
ArrayList<Integer> evens = new ArrayList<>();
for (int num : arr) {
    if (num % 2 == 0) {
        evens.add(num);
    }
}

Description:
Accessing every element in a 2D array.
for (int r = 0; r < matrix.length; r++) {
    for (int c = 0; c < matrix[0].length; c++) {
        // process matrix[r][c]
    }
}

Description:
Exchanging two elements, often in sorting.
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;

Description:
Finding the index of a target value in an array or ArrayList.
for (int i = 0; i < arr.length; i++) {
    if (arr[i] == target) {
        // found at i
    }
}

Description:
Accessing characters or substrings.
for (int i = 0; i < str.length(); i++) {
    char ch = str.charAt(i);
}

Description:
Adding, removing, or accessing elements.
list.add(value);
list.remove(index);
list.get(index);

Description:
Accumulating a total.
int sum = 0;
for (int num : arr) {
    sum += num;
}

Description:
Using a flag to indicate a condition has been met.
boolean found = false;
for (int num : arr) {
    if (num == target) {
        found = true;
        break;
    }
}

Description:
Iterating backward through an array or ArrayList.
for (int i = arr.length - 1; i >= 0; i--) {
    // process arr[i]
}

Description:
Finding min/max and swapping in a sort step.
for (int i = 0; i < arr.length - 1; i++) {
    int minIndex = i;
    for (int j = i + 1; j < arr.length; j++) {
        if (arr[j] < arr[minIndex]) {
            minIndex = j;
        }
    }
    // swap arr[i] and arr[minIndex]
}

Description:
Initializing fields in a new object.
public class MyClass {
    private int x;
    public MyClass(int val) {
        x = val;
    }
}

Description:
Writing a method that returns a computed value.
public int sum(int a, int b) {
    return a + b;
}

Description:
Extending a class and providing a new implementation.
public class Dog extends Animal {
    @Override
    public void speak() {
        System.out.println("Woof!");
    }
}

Description:
Recognizing class-level (static) and object-level (instance) methods.
public static int add(int a, int b) { ... }
public int getValue() { ... }

Description:
Safely removing elements while iterating.
for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i) < 0) {
        list.remove(i);
    }
}

Description:
Comparing object or string values correctly.
if (str1.equals(str2)) { ... }
if (obj1 == obj2) { ... }

Description:
Efficiently finding a target in a sorted array using divide-and-conquer.
int low = 0, high = arr.length - 1;
while (low <= high) {
    int mid = (low + high) / 2;
    if (arr[mid] == target) return mid;
    else if (arr[mid] < target) low = mid + 1;
    else high = mid - 1;
}

Description:
Inserting one element into its correct position in a sorted portion.
for (int i = 1; i < arr.length; i++) {
    int key = arr[i];
    int j = i - 1;
    while (j >= 0 && arr[j] > key) {
        arr[j + 1] = arr[j];
        j--;
    }
    arr[j + 1] = key;
}

Description:
Logical equivalences for negating compound boolean expressions.
!(a && b) equals (!a || !b)
!(a || b) equals (!a && !b)

// Example:
if (!(x > 5 && y < 10)) { ... }
// is equivalent to:
if (x <= 5 || y >= 10) { ... }

Description:
Generating random integers within a specific range [min, max].
// Random integer from min to max (inclusive)
int randomNum = (int)(Math.random() * (max - min + 1)) + min;

// Example: Random number from 1 to 6 (dice roll)
int dice = (int)(Math.random() * 6) + 1;

Description:
Finding the middle value in a sorted array or ArrayList.
// Assuming arr is already sorted
if (arr.length % 2 == 1) {
    // Odd length: return middle element
    median = arr[arr.length / 2];
} else {
    // Even length: return average of two middle elements
    int mid1 = arr[arr.length / 2 - 1];
    int mid2 = arr[arr.length / 2];
    median = (mid1 + mid2) / 2.0;
}

Description:
Processing each row individually to calculate row-based statistics.
for (int r = 0; r < matrix.length; r++) {
    int rowSum = 0;
    for (int c = 0; c < matrix[r].length; c++) {
        rowSum += matrix[r][c];
    }
    // process rowSum
}

Description:
Processing each column individually to calculate column-based statistics.
for (int c = 0; c < matrix[0].length; c++) {
    int colSum = 0;
    for (int r = 0; r < matrix.length; r++) {
        colSum += matrix[r][c];
    }
    // process colSum
}

Description:
Checking adjacent cells (up, down, left, right) with bounds checking.
int[][] directions = {{-1,0}, {1,0}, {0,-1}, {0,1}};
for (int[] dir : directions) {
    int newR = r + dir[0];
    int newC = c + dir[1];
    if (newR >= 0 && newR < matrix.length && 
        newC >= 0 && newC < matrix[0].length) {
        // process matrix[newR][newC]
    }
}

Description:
Traversing a 2D array when you only need values, not indices.
for (int[] row : matrix) {
    for (int value : row) {
        // process value
    }
}

Description:
A method that calls itself with a simpler version of the problem.
public int factorial(int n) {
    if (n <= 1) {        // base case
        return 1;
    }
    return n * factorial(n - 1);  // recursive call
}

// Another common pattern: countdown
public void countdown(int n) {
    if (n <= 0) return;  // base case
    System.out.println(n);
    countdown(n - 1);    // recursive call
}
Curated for APCS-A students | Last updated: 3/10/2026 | Powered by Bootstrap 5
Back to TNT's APCS Test Review