Java ArrayList, explained for AP Computer Science A
An ArrayList is a resizable list of objects. It is one of the most tested topics in AP Computer Science A, and at least one free-response question every year usually involves one.
On this page
What an ArrayList is
A normal Java array has a fixed length that you choose when you create it. An ArrayList can grow and shrink as you add and remove elements, which makes it ideal when you do not know in advance how many items you will store. It lives in the java.util package, so a program needs import java.util.ArrayList; at the top. On the AP exam, imports are usually assumed.
An ArrayList can only hold objects, not primitive types. To store whole numbers you use the wrapper class Integer; for decimals, Double. Java converts between int and Integer automatically, a feature called autoboxing and unboxing.
Creating an ArrayList
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> scores = new ArrayList<>();
The type in angle brackets is the type of element the list holds. In the second line, the empty brackets <> (the diamond) let Java infer the type. A new list starts empty, with size 0.
The AP subset methods
The AP Java Quick Reference lists the ArrayList methods you need. Know exactly what each one returns:
int size(): the number of elements.boolean add(E obj): appends obj to the end and returns true.void add(int index, E obj): inserts obj at index, shifting later elements right.E get(int index): returns the element at index.E set(int index, E obj): replaces the element at index and returns the old element.E remove(int index): removes the element at index, shifts later elements left, and returns the removed element.
Indexes run from 0 to size() − 1. Using an index outside that range throws an IndexOutOfBoundsException.
Worked examples
Example 1: tracing method calls
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add(0, "C");
list.set(2, "D");
list.remove(1);- After the adds: [A, B]. Inserting C at index 0: [C, A, B].
- set(2, "D") replaces B: [C, A, D]. remove(1) removes A.
- Final list: [C, D].
Example 2: summing with a for-each loop
Write code that returns the sum of an ArrayList<Integer> called nums.
int total = 0; for (int n : nums) { total += n; } return total;- The enhanced for loop visits every element. Each Integer is unboxed to an int automatically.
Example 3: finding the largest value
Write a method that returns the largest value in a non-empty ArrayList<Integer>.
public static int findMax(ArrayList<Integer> nums) { int max = nums.get(0); for (int i = 1; i < nums.size(); i++) { if (nums.get(i) > max) { max = nums.get(i); } } return max; }- Start with the first element rather than 0, so the method also works when every value is negative.
Removing elements safely
Removing inside a loop is the most common ArrayList trap on the AP exam. When you remove the element at index i, every later element shifts left by one. If the loop then does i++, it skips the element that just moved into position i.
Example 4: removing all negative numbers
Remove every negative value from an ArrayList<Integer> called nums.
- Option 1: loop backwards. Shifting only affects elements you have already checked.
for (int i = nums.size() - 1; i >= 0; i--) { if (nums.get(i) < 0) { nums.remove(i); } } - Option 2: only increase i when nothing is removed.
int i = 0; while (i < nums.size()) { if (nums.get(i) < 0) { nums.remove(i); } else { i++; } } - Never remove inside an enhanced for loop: it throws a
ConcurrentModificationException.
Comparing Integer objects
Because an ArrayList stores Integer objects, comparing two elements with == compares whether they are the same object, not whether they hold the same value. For small values Java often reuses the same objects, so == may appear to work in testing and then fail for larger numbers. Safe options are to unbox into int variables first, for example int a = list.get(0);, or to use list.get(0).equals(list.get(1)). Comparisons with < and > are fine, because they unbox automatically. The same rule applies to Strings: use equals, not ==.
ArrayLists of your own objects
Many FRQs use an ArrayList of objects from a class described in the question, such as ArrayList<Student>. Access fields through the class's methods: roster.get(i).getGrade(). Read the class description carefully, and only call methods that it lists.
ArrayList versus arrays
- Size: arrays are fixed; ArrayLists grow and shrink.
- Length: arrays use
arr.length(no brackets); ArrayLists uselist.size(). - Access: arrays use
arr[i]; ArrayLists uselist.get(i)andlist.set(i, x). - Types: arrays can hold primitives; ArrayLists hold objects only.
Mixing up length, length() and size() is a classic error: arrays use length, Strings use length(), and ArrayLists use size().
Common FRQ patterns
Free-response questions often ask you to filter a list (keep or remove elements that meet a condition), build a new list from an existing one, count elements with a property, or find an element and return its index. Read the question carefully to see whether it wants the original list changed or a new list returned. Use the class's existing methods as described, and do not rewrite methods you are told already work.
Common mistakes
- Using list.length or list[i] instead of list.size() and list.get(i).
- Skipping elements when removing in a forward loop.
- Removing inside an enhanced for loop.
- Forgetting that set and remove return the old or removed element.
- Declaring ArrayList<int>, which does not compile; use ArrayList<Integer>.
Practice questions
Try each one before opening the answer.
Q1. A list holds [5, 8, 2]. What does list.set(1, 9) return, and what is the list afterwards?
Show answer
Q2. A list holds ["x", "y", "z"]. After list.add(1, "w"), what is list.get(2)?
Show answer
Q3. What is wrong with: for (int i = 0; i < list.size(); i++) { if (list.get(i) == 0) list.remove(i); }?
Show answer
Q4. Write one line that adds the number 7 to the end of an ArrayList<Integer> called nums.
Show answer
Frequently asked questions
Is ArrayList on the AP Computer Science A exam?
Yes. ArrayList is a central part of the Data Collections unit in the revised AP CSA course, and it appears in both multiple-choice and free-response questions.
Do I need to know other ArrayList methods like contains or indexOf?
Use the methods listed on the current AP Java Quick Reference, which you get in the exam. Whatever is listed, you can always write searches like contains or indexOf yourself with a loop, and free-response questions often expect exactly that.
Key takeaways
- ArrayLists resize; arrays do not.
- Use size(), get(i), set(i, x), add and remove.
- Store objects such as Integer, not primitives.
- Loop backwards when removing elements.
- Never remove inside an enhanced for loop.