AP Computer Science A Unit 7: ArrayList
Study ArrayList methods, traversal, searching, sorting, autoboxing with exam-format practice and rubric-based scoring.
Start AP CSA Practice · Full Study Guide
AP and Advanced Placement are trademarks of College Board. AimFive is not affiliated with or endorsed by College Board.
Inside This Unit: The Full Breakdown
ArrayList is a resizable collection from the Java API that stores objects. This unit covers adding, removing, accessing, and traversing elements, as well as the differences between ArrayList and arrays.
Why it matters
ArrayList appears on every AP CSA exam. Its ability to grow and shrink dynamically makes it more flexible than arrays. Understanding how add, remove, get, and set interact — especially the index-shifting behavior of remove — is essential.
Key concepts
- ArrayList stores objects only (not primitives). Use Integer, Double, Boolean wrapper classes.
- add(element) appends to the end; add(index, element) inserts at a position, shifting subsequent elements.
- remove(index) removes and shifts elements left; get(index) returns the element at that position.
- When removing elements during traversal, iterate backward or adjust the index to avoid skipping.
ArrayList Basics
An ArrayList is created with a type parameter: ArrayList<String> names = new ArrayList<String>(). Unlike arrays, ArrayList starts empty and grows as you add elements. The add method appends to the end by default. The size() method returns the current number of elements (analogous to array's length). Because ArrayList stores objects, primitive values are autoboxed: list.add(5) automatically converts 5 to an Integer object. ArrayList is in java.util and must be imported.
Modifying and Accessing Elements
The get(index) method returns the element at the specified index. The set(index, element) method replaces the element at that index. The add(index, element) method inserts at the given index, shifting all subsequent elements one position to the right. The remove(index) method removes the element and shifts all subsequent elements one position to the left, decreasing size by 1. Understanding these shifts is critical for the AP exam, which frequently tests what happens when you remove elements during a traversal.
Traversal Pitfalls
When removing elements from an ArrayList in a forward loop, the indices shift after each removal, causing elements to be skipped. Two solutions: traverse backward so that shifts only affect already-processed elements, or decrement the loop variable after each removal. An enhanced for loop cannot safely remove elements during traversal — it throws a ConcurrentModificationException. The AP exam tests these scenarios repeatedly. Always trace through your loop to verify that every element is visited exactly once.
AP exam tip
When a free-response question asks you to remove all elements meeting a condition, iterate backward: for (int i = list.size()-1; i >= 0; i--). This avoids the index-shifting problem entirely.
Connections to other units
- Unit 5: Arrays and ArrayLists serve similar purposes but differ in flexibility and syntax.
- Unit 4: Traversal patterns (for loops, enhanced for loops) apply to both arrays and ArrayLists.
- Unit 9: ArrayLists of superclass types can hold subclass objects, enabling polymorphism.