Linear search

java
public static int search(ArrayList<String> list, String target) {
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).equals(target)) {
            return i;
        }
    }
    return -1;  // not found
}
java
ArrayList<String> fruits = new ArrayList<>();
fruits.add("apple"); fruits.add("banana"); fruits.add("cherry");

System.out.println(search(fruits, "banana")); // 1
System.out.println(search(fruits, "grape"));  // -1

Finding min and max with objects

java
public static String findLongest(ArrayList<String> words) {
    String longest = words.get(0);
    for (String w : words) {
        if (w.length() > longest.length()) {
            longest = w;
        }
    }
    return longest;
}
java
ArrayList<String> words = new ArrayList<>();
words.add("cat"); words.add("elephant"); words.add("dog");
System.out.println(findLongest(words));  // "elephant"

Selection sort with ArrayList

java
public static void selectionSort(ArrayList<Integer> list) {
    for (int i = 0; i < list.size() - 1; i++) {
        int minIdx = i;
        for (int j = i + 1; j < list.size(); j++) {
            if (list.get(j) < list.get(minIdx)) {
                minIdx = j;
            }
        }
        // Swap using set
        int temp = list.get(i);
        list.set(i, list.get(minIdx));
        list.set(minIdx, temp);
    }
}

Insertion sort with ArrayList

java
public static void insertionSort(ArrayList<Integer> list) {
    for (int i = 1; i < list.size(); i++) {
        int key = list.get(i);
        int j = i - 1;
        while (j >= 0 && list.get(j) > key) {
            list.set(j + 1, list.get(j));
            j--;
        }
        list.set(j + 1, key);
    }
}

Removing all matching elements

java
public static void removeAll(ArrayList<String> list, String target) {
    for (int i = list.size() - 1; i >= 0; i--) {
        if (list.get(i).equals(target)) {
            list.remove(i);
        }
    }
}
java
ArrayList<String> items = new ArrayList<>();
items.add("a"); items.add("b"); items.add("a"); items.add("c"); items.add("a");
removeAll(items, "a");
System.out.println(items);  // [b, c]

Removing consecutive duplicates

java
public static void removeConsecutiveDups(ArrayList<String> list) {
    for (int i = list.size() - 1; i > 0; i--) {
        if (list.get(i).equals(list.get(i - 1))) {
            list.remove(i);
        }
    }
}
java
// ["a", "a", "b", "b", "b", "c"] → ["a", "b", "c"]

Converting between Array and ArrayList

Array → ArrayList

java
int[] arr = {10, 20, 30, 40};
ArrayList<Integer> list = new ArrayList<>();
for (int val : arr) {
    list.add(val);  // autoboxing: int → Integer
}

ArrayList → Array

java
ArrayList<Integer> list = new ArrayList<>();
list.add(10); list.add(20); list.add(30);

int[] arr = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
    arr[i] = list.get(i);  // unboxing: Integer → int
}

Merging two sorted lists

java
public static ArrayList<Integer> merge(ArrayList<Integer> a, ArrayList<Integer> b) {
    ArrayList<Integer> result = new ArrayList<>();
    int i = 0, j = 0;
    
    while (i < a.size() && j < b.size()) {
        if (a.get(i) <= b.get(j)) {
            result.add(a.get(i));
            i++;
        } else {
            result.add(b.get(j));
            j++;
        }
    }
    
    while (i < a.size()) { result.add(a.get(i)); i++; }
    while (j < b.size()) { result.add(b.get(j)); j++; }
    
    return result;
}

FRQ-style example: GradeBook

java
import java.util.ArrayList;

public class GradeBook {
    private ArrayList<Integer> scores;
    
    public GradeBook() {
        scores = new ArrayList<>();
    }
    
    public void addScore(int score) {
        scores.add(score);
    }
    
    public double getAverage() {
        int sum = 0;
        for (int s : scores) {
            sum += s;
        }
        return (double) sum / scores.size();
    }
    
    public int getHighest() {
        int max = scores.get(0);
        for (int s : scores) {
            if (s > max) max = s;
        }
        return max;
    }
    
    /** Removes all scores below the threshold.
     *  Returns the number of scores removed. */
    public int dropLow(int threshold) {
        int count = 0;
        for (int i = scores.size() - 1; i >= 0; i--) {
            if (scores.get(i) < threshold) {
                scores.remove(i);
                count++;
            }
        }
        return count;
    }
    
    /** Returns a new list of scores that appear more than once */
    public ArrayList<Integer> getDuplicates() {
        ArrayList<Integer> dups = new ArrayList<>();
        for (int i = 0; i < scores.size(); i++) {
            for (int j = i + 1; j < scores.size(); j++) {
                if (scores.get(i).equals(scores.get(j)) && !dups.contains(scores.get(i))) {
                    dups.add(scores.get(i));
                }
            }
        }
        return dups;
    }
}

AP Exam Tips

Common Mistakes

Key Vocabulary