AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/4/4-8)
Unit 4 · Topic 4.8
4.8 ArrayList Methods
An ArrayList is a list that grows and shrinks as you add and remove elements. This topic covers creating one, the six methods on the Java Quick Reference, and how adding or removing in the middle shifts every element after it.
Key terms
ArrayList- generic type
<E> sizeaddgetandsetremove
Arrays versus ArrayLists
An array's length is fixed when it's created. An ArrayList is mutable in size: it starts empty and grows or shrinks as you add or remove elements. It holds object references only, so numbers go in as Integer or Double (4.7).
ArrayList is in the java.util package, so you need import java.util.ArrayList; (or import java.util.*;).
Creating one
ArrayList<E> is a generic type: E stands for the type of the elements, written in angle brackets. Always include it:
ArrayList<String> words = new ArrayList<String>();makes an empty list of strings.- With the type given, the compiler checks every
addandget, so mistakes like adding a number to a list of strings are caught before the program runs.
The six methods
Indexes start at 0 and end at size() - 1, just like arrays.
| Method | What it does | Returns |
|---|---|---|
| size() | counts the elements | the number of elements, an int |
| add(obj) | adds obj at the end | true |
| add(index, obj) | inserts obj at index, shifting elements from index on one place right | nothing (void) |
| get(index) | looks up an element | the element at index |
| set(index, obj) | replaces the element at index with obj | the element that was there before |
| remove(index) | removes the element at index, shifting later elements one place left | the element that was removed |
Shifting indexes
Inserting or removing in the middle moves everything after that spot. After add(0, "ant"), every element that was already in the list has an index 1 higher than before. After remove(1), every element after index 1 has an index 1 lower. That's very convenient, but it's also why traversals that remove elements need care (4.9).
For add(index, obj), the index can be anything from 0 to size(), inclusive. Using size() adds at the end. For get, set and remove, the index must be from 0 to size() - 1. Any other index throws an IndexOutOfBoundsException.
Remember the differences from arrays: size() instead of length, and get(i) and set(i, x) instead of square brackets.
Lists of objects
On the exam, lists usually hold objects of a class the question describes, like ArrayList<Book>. Then get(i) returns a reference to a Book, and you can call its methods right away: books.get(i).getPages(). Read that from left to right: get the element at index i, then ask it for its page count. Since the list holds references, calling a mutator on books.get(i) changes the object that's in the list.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Tracing list operations
What does this code print? (Printing an
ArrayListshows its elements in square brackets.)ArrayList<String> words = new ArrayList<String>(); words.add("cat"); words.add("dog"); words.add(0, "ant"); words.add("eel"); String old = words.set(2, "fox"); String gone = words.remove(1); System.out.println(words + " " + words.size()); System.out.println(old + " " + gone + " " + words.get(1));Show the solutionHide the solution
- Step 1: After the first two
adds: [cat, dog]. - Step 2:
add(0, "ant")inserts at the front and shifts the others right: [ant, cat, dog]. - Step 3:
add("eel")appends: [ant, cat, dog, eel]. - Step 4:
set(2, "fox")replaces dog with fox and returns the old element, sooldis "dog". The list is [ant, cat, fox, eel]. - Step 5:
remove(1)removes cat and returns it, sogoneis "cat". The later elements shift left: [ant, fox, eel], size 3. - Step 6:
words.get(1)is now "fox".
Answer: Two lines:
[ant, fox, eel] 3, thendog cat fox. - Step 1: After the first two
- Example 2
Valid insert positions
What does this code print?
ArrayList<Double> temps = new ArrayList<Double>(); temps.add(71.5); temps.add(68.0); temps.add(temps.size(), 74.0); System.out.println(temps);Show the solutionHide the solution
- Step 1: After two
adds the list is [71.5, 68.0], sosize()is 2. - Step 2:
add(2, 74.0)inserts at index 2. That's equal tosize(), which is allowed and means "at the end". - Step 3: By contrast, inserting at index 3 here would throw an
IndexOutOfBoundsException, because it's more thansize().
Answer: It prints
[71.5, 68.0, 74.0]. - Step 1: After two
Common mistakes
- Using
lengthor square brackets with anArrayList. Usesize(),get(i)andset(i, x). - Forgetting that
add(index, obj)andremove(index)shift the indexes of every later element. - Forgetting that
setandremovereturn the old element, which tracing questions often print.
On the exam
- Since the 2025 course update, free-response Question 3 (Data Analysis with ArrayList) is worth 5 points instead of 9. You'll call these methods on an
ArrayListof objects, so know exactly what each one returns.
Connected topics
Videos
Check yourself
4 questions on 4.8 ArrayList Methods. Pick an answer to see if you got it, and why.
Consider the following code segment.ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add(0, "C");
list.set(1, "D");
list.remove(2);
list.add("E");Which of the following represents the contents of list after the code segment executes?
Consider the following code segment.ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(10);
nums.add(20);
nums.add(30);
int x = nums.remove(1);
System.out.println(x + " " + nums.size() + " " + nums.get(1));What is printed as a result of executing the code segment?
Consider the following code segment.ArrayList<String> pets = new ArrayList<String>();
pets.add("cat");
pets.add("dog");
String old = pets.set(0, "fish");
System.out.println(old + " " + pets.get(0) + " " + pets.size());What is printed as a result of executing the code segment?
Consider the following code segment.ArrayList<String> list = new ArrayList<String>();
list.add("x");
list.add("y");
list.add(3, "z");
System.out.println(list.size());What is printed as a result of executing the code segment?
0 of 4 answered