AP Computer Science A: ArrayList — AP Computer Science A
1. Introduction to ArrayList: Creation & Syntax ★★☆☆☆ ⏱ 4 min
ArrayList is Java's resizable array implementation from the `java.util` Collections framework, designed for ordered collections where the number of elements changes at runtime. Unlike fixed-size arrays, it automatically adjusts capacity when elements are added/removed, eliminating manual reallocation. It makes up 7-10% of your AP CS A score, appearing in at least one FRQ annually.
ArrayList uses generic type parameters, which *must* be reference types (primitives like `int` are not allowed). For primitives, use the corresponding wrapper class: `Integer` for `int`, `Double` for `double`, `Character` for `char`, and `Boolean` for `boolean`.
2. ArrayList vs Standard Fixed-Size Arrays ★★☆☆☆ ⏱ 3 min
The differences between ArrayList and standard arrays are one of the most frequently tested multiple-choice topics on the AP CS A exam. Key distinctions are summarized below:
3. Core Tested ArrayList Methods ★★★☆☆ ⏱ 4 min
The AP CS A exam explicitly tests 5 core ArrayList methods that you must memorize perfectly. All methods throw an `IndexOutOfBoundsException` if you pass an invalid index (negative or greater than/equal to the list size, except as noted below):
- **`int size()`**: Returns the number of elements in the list (0 for an empty list)
- **`E get(int index)`**: Returns the element stored at the specified 0-indexed position
- **`E set(int index, E newValue)`**: Replaces the element at the specified index, returns the old value that was replaced
- **`boolean add(E value)`**: Appends value to the end of the list, increases size by 1, returns `true`. Overload: `void add(int index, E value)` inserts value at index, shifting all subsequent elements right.
- **`E remove(int index)`**: Removes element at index, shifts subsequent elements left, decreases size, returns removed element. Overload: `boolean remove(E value)` removes first occurrence of value, returns `true` if found and removed.
4. Traversal & Common Tested Algorithms ★★★☆☆ ⏱ 5 min
The for-each (enhanced for) loop is the simplest way to traverse an ArrayList when you only need to read elements, not modify the list structure. Syntax:
```java for (ElementType elementVariable : listName) { // Code to process each element } ```
- Advantages: No index variable to manage, eliminates off-by-one errors, clean and readable, automatic unboxing of wrapper types to primitives
- Critical Limitations: You cannot add or remove elements during for-each traversal (throws `ConcurrentModificationException`), cannot access the current element index, cannot traverse in reverse.
The following common ArrayList algorithms are tested repeatedly on both multiple-choice and FRQ sections, and you should be able to write each from memory:
Common Pitfalls
Why: Missing the import causes a compilation error, which loses points on FRQs.
Why: ArrayList only accepts reference type parameters; primitives are not allowed.
Why: The last valid index is `list.size() - 1`, so this causes an `IndexOutOfBoundsException`.
Why: The `remove` overload for `int` matches the index parameter first, so this removes the element at index 3 instead.
Why: This throws a `ConcurrentModificationException` at runtime.