Lesson 4.8: ArrayList Methods
Lesson 4.8: ArrayList Methods
What You'll Learn
- 4.8.A: Call ArrayList methods to manipulate a list.
- Add elements to the end and at a specific index.
- Remove elements by index and understand how the list shifts after removal.
- Get and set elements by index.
- Check size and determine whether the list contains a value.
📌 These Methods Are on the AP Reference Sheet
The six ArrayList methods below appear verbatim on the AP Java Quick Reference you receive during the exam. You don't need to memorize every detail — but you must know what each method does, what it returns, and how the list changes after each call.
ArrayList Methods — AP Reference Sheet
| Method | Returns | Description |
|---|---|---|
| add(E obj) | void | Appends obj to the end of the list |
| add(int index, E obj) | void | Inserts obj at index; shifts existing elements right |
| remove(int index) | E | Removes and returns element at index; shifts remaining elements left |
| get(int index) | E | Returns element at index without removing it |
| set(int index, E obj) | E | Replaces element at index with obj; returns the old element |
| size() | int | Returns the number of elements in the list |
add — Appending and Inserting
ArrayList list = new ArrayList();
list.add("Alice"); // list: ["Alice"]
list.add("Carlos"); // list: ["Alice", "Carlos"]
list.add("Diana"); // list: ["Alice", "Carlos", "Diana"]
// Insert at index 1 — shifts "Carlos" and "Diana" right
list.add(1, "Ben"); // list: ["Alice", "Ben", "Carlos", "Diana"]
⚠️ AP Exam Trap: Inserting Shifts Everything Right
When you call add(index, obj), every element from that index onward moves one position to the right. The size increases by 1. Students often forget this and trace the wrong indices after an insert.
remove — Removing by Index
// list: ["Alice", "Ben", "Carlos", "Diana"]
String removed = list.remove(1);
// removed = "Ben"
// list: ["Alice", "Carlos", "Diana"] ← shifted left
remove(int index) removes the element at that index, shifts all elements after it one position left, decrements the size by 1, and returns the removed element.
⚠️ AP Exam Trap: Removing While Traversing
If you remove elements from an ArrayList inside a standard for loop using an index, the indices shift after each removal and you can skip elements. This is covered in Lesson 4.9. For now: removing shifts elements left and shrinks the list.
get and set — Reading and Replacing
// list: ["Alice", "Carlos", "Diana"]
String first = list.get(0); // "Alice" — list unchanged
list.set(1, "Carmen"); // replaces "Carlos" with "Carmen"
// list: ["Alice", "Carmen", "Diana"]
get(index) reads without changing the list. set(index, obj) replaces the element and returns the old value — the list size stays the same.
size — Current Element Count
ArrayList nums = new ArrayList();
nums.add(10);
nums.add(20);
nums.add(30);
System.out.println(nums.size()); // 3
nums.remove(0);
System.out.println(nums.size()); // 2 — decreased after removal
✅ Example: Building a Filtered List
// Start with an array, build an ArrayList of only even values
int[] data = {3, 8, 5, 12, 7, 4};
ArrayList evens = new ArrayList();
for (int x : data) {
if (x % 2 == 0) {
evens.add(x);
}
}
// evens: [8, 12, 4]
System.out.println(evens.size()); // 3
This pattern — start with an empty ArrayList, loop over a source, add qualifying elements — is the standard way to filter data in Java.
Summary
-
add(obj)appends to the end.add(index, obj)inserts and shifts elements right. -
remove(index)removes, shifts elements left, and returns the removed element. -
get(index)reads an element without modifying the list. -
set(index, obj)replaces an element and returns the old one — size stays the same. -
size()returns the current number of elements. - All six methods are on the AP Java Quick Reference sheet.
Practice Questions
ArrayListlist = new ArrayList (); list.add(10); list.add(20); list.add(30); System.out.println(list.get(1));
get(1) returns 20. ArrayLists are 0-indexed just like arrays.ArrayListlist = new ArrayList (); list.add("A"); list.add("B"); list.add("C"); list.add(1, "X");
add(1, "X") inserts "X" at index 1, shifting "B" and "C" right. Result: ["A", "X", "B", "C"]. A would be correct if "X" replaced "B" (that's set, not add). D would be appending to the end.ArrayListlist = new ArrayList (); list.add("red"); list.add("green"); list.add("blue"); list.remove(0); System.out.println(list.get(0));
remove(0) removes "red" and shifts everything left. The list becomes ["green", "blue"]. get(0) now returns "green".set return?
ArrayListlist = new ArrayList (); list.add("cat"); list.add("dog"); String old = list.set(0, "bird"); System.out.println(old);
set(0, "bird") replaces "cat" with "bird" and returns the element that was replaced — "cat". After the call the list is ["bird", "dog"], but old holds the original value.ArrayListlist = new ArrayList (); list.add(5); list.add(10); list.add(15); list.add(1, 7); list.remove(2); System.out.println(list.size() + " " + list.get(1));
Mastery: ArrayList Methods
ArrayListlist = new ArrayList (); list.add(1); list.add(2); list.add(3); list.add(4); list.remove(1); list.add(1, 9); System.out.println(list);
list with the value 99?list.set(list.size(), 99);
list.add(list.size(), 99);
list.set(list.size() + 1, 99);
list.set(list.size() - 1, 99);
size() - 1. A uses size() as the index — out of bounds. B uses add at size() — that appends a new element rather than replacing. C uses size() + 1 — further out of bounds.String first = list.remove(0); list.add(first);
list.add(list.get(0)); list.remove(0);
list.set(list.size() - 1, list.get(0));
list.add(0, list.remove(list.size() - 1));
ArrayListlist = new ArrayList (); list.add(10); list.add(20); list.add(30); list.set(1, list.get(0) + list.get(2)); System.out.println(list.get(1));
list.get(0) = 10, list.get(2) = 30. 10 + 30 = 40. set(1, 40) replaces the value at index 1 (was 20) with 40. get(1) now returns 40.list contain?
ArrayListlist = new ArrayList (); list.add("W"); list.add("X"); list.add("Y"); list.add("Z"); list.remove(list.size() - 1); list.add(0, list.remove(1));
list.remove(1) before executing list.add(0,...), so the remove happens first.Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]