4.08 ArrayList Methods
Use ArrayList methods to add, remove, replace, access, and measure elements in a resizable collection.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Tech Talk
- Code Runner Challenge
- Code Examples
- A. List basics. The ListBasics class adds, reads, replaces, and counts
- Code Runner Challenge
- B. Two remove methods. The RemoveComparison class removes by position and by value
- Code Runner Challenge
- Hacks & Practice Tasks
- Popcorn Hack
- Code Runner Challenge
- MCQ Check
- Homework Hack
- References
Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| ArrayList | A list of objects that grows and shrinks. | ArrayList<String> names = new ArrayList<String>(); |
| add | Appends to the end, or inserts at an index. | names.add("Ana"); names.add(0, "Bo"); |
| get | Reads the element at an index. | names.get(0) |
| set | Replaces the element at an index, returns the old one. | names.set(1, "Cy"); |
| remove | Deletes by index or by value, shifting later elements left. | names.remove(0); |
| size | Number of elements right now. | names.size() |
- Valid indexes run from
0tosize() - 1. Anything else throwsIndexOutOfBoundsException. - An
ArrayListholds objects, so useArrayList<Integer>, neverArrayList<int>.
Array vs. ArrayList
| Array | ArrayList | |
|---|---|---|
| Size | Fixed | Grows and shrinks |
| Count | arr.length |
list.size() |
| Read / write | arr[i] / arr[i] = v |
list.get(i) / list.set(i, v) |
Picking a remove
- Remove by position →
nums.remove(2) - Remove by value →
nums.remove(Integer.valueOf(2))
College Board Requirements
AP CSA Unit 4, Topic 4.8 ArrayList Methods. Quoted from the course and exam description (College Board, 2025, pp. 110-111):
- 4.8.A.1 “An
ArrayListobject is mutable in size and contains object references.” - 4.8.A.3 “
ArrayList<E>is preferred overArrayList. For example,ArrayList<String> names = new ArrayList<String>();allows the compiler to find errors that would otherwise be found at run-time.” - 4.8.A.6 “The indices for an
ArrayListstart at 0 and end at the number of elements - 1.”
The methods in the Key Topics table are the ones on the Java Quick Reference (EK 4.8.A.5).
Lesson Plan
Learning Objective: Build and change an ArrayList with add, get, set, remove, and size.
Success Criteria: You can predict a list after each method call, and choose remove(int index) or remove(Object obj) on purpose.
Tech Talk
An array is a row of fixed boxes. An ArrayList is a grocery list: it grows when you add and shrinks when you cross something off.
Predict each line before running, then change get(3) to get(2)
// add, get, set, size
import java.util.ArrayList;
public class ListBasics {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(70);
scores.add(90);
scores.add(1, 85); // insert, 90 shifts right
System.out.println(scores + ", size " + scores.size());
int old = scores.set(1, 95); // returns the replaced value
System.out.println("replaced " + old + " -> " + scores);
try {
System.out.println(scores.get(3));
} catch (IndexOutOfBoundsException e) {
System.out.println("get(3) failed: " + e.getMessage());
}
}
}
Code Runner Challenge
Predict each line before running, then change get(3) to get(2)
View IPYNB Source
ArrayList<String> groceries = new ArrayList<String>();
groceries.add("Milk"); // [Milk]
groceries.add(0, "Eggs"); // [Eggs, Milk]
groceries.set(1, "Oat Milk"); // [Eggs, Oat Milk]
groceries.remove(0); // [Oat Milk]
groceries.size(); // 1
Code Examples
A. List basics. The ListBasics class adds, reads, replaces, and counts
Array habits that break:
scores[1] = 95; // does not compile, use scores.set(1, 95)
scores.length // does not compile, use scores.size()
Terminology:
get and set never change the size. Only add and remove do.
Predict both lists before running, then try removing Integer.valueOf(9)
// remove(int index) vs. remove(Object obj)
import java.util.ArrayList;
public class RemoveComparison {
public static void main(String[] args) {
ArrayList<Integer> byIndex = new ArrayList<Integer>();
byIndex.add(1);
byIndex.add(2);
byIndex.add(3);
byIndex.remove(1);
System.out.println("byIndex = " + byIndex);
ArrayList<Integer> byValue = new ArrayList<Integer>();
byValue.add(1);
byValue.add(2);
byValue.add(3);
byValue.remove(Integer.valueOf(1));
System.out.println("byValue = " + byValue);
}
}
Code Runner Challenge
Predict both lists before running, then try removing Integer.valueOf(9)
View IPYNB Source
// CODE_RUNNER: Predict each line before running, then change get(3) to get(2)
// add, get, set, size
import java.util.ArrayList;
public class ListBasics {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(70);
scores.add(90);
scores.add(1, 85); // insert, 90 shifts right
System.out.println(scores + ", size " + scores.size());
int old = scores.set(1, 95); // returns the replaced value
System.out.println("replaced " + old + " -> " + scores);
try {
System.out.println(scores.get(3));
} catch (IndexOutOfBoundsException e) {
System.out.println("get(3) failed: " + e.getMessage());
}
}
}
ListBasics.main(null);
B. Two remove methods. The RemoveComparison class removes by position and by value
Common remove mistake:
nums.remove(1); // int: removes the element AT index 1
nums.remove(Integer.valueOf(1)); // Integer: removes the VALUE 1
Terminology:
Java picks the method from the argument type. An int means index; an Integer means value.
Replace the sample values with your own, then run it
// Practice #1 - Try it yourself first!
import java.util.ArrayList;
public class MyPlaylist {
public static void main(String[] args) {
// Try writing your own code here first!
// Sample answer:
ArrayList<String> songs = new ArrayList<String>();
songs.add("Golden Hour");
songs.add("Levitating");
songs.add("Sunflower");
songs.add(0, "Espresso");
songs.set(songs.size() - 1, "Heat Waves");
songs.remove("Levitating");
System.out.println(songs + ", " + songs.size() + " songs");
}
}
Code Runner Challenge
Replace the sample values with your own, then run it
View IPYNB Source
// CODE_RUNNER: Predict both lists before running, then try removing Integer.valueOf(9)
// remove(int index) vs. remove(Object obj)
import java.util.ArrayList;
public class RemoveComparison {
public static void main(String[] args) {
ArrayList<Integer> byIndex = new ArrayList<Integer>();
byIndex.add(1);
byIndex.add(2);
byIndex.add(3);
byIndex.remove(1);
System.out.println("byIndex = " + byIndex);
ArrayList<Integer> byValue = new ArrayList<Integer>();
byValue.add(1);
byValue.add(2);
byValue.add(3);
byValue.remove(Integer.valueOf(1));
System.out.println("byValue = " + byValue);
}
}
RemoveComparison.main(null);
Hacks & Practice Tasks
Popcorn Hack
2-minute challenge: build a playlist, then run the cell.
- Add three songs
- Insert a favorite at index 0
- Replace the last song (use
size() - 1) - Remove one song, then print the list and its size
Replace the sample values with your own.
Fill in the blanks, then run it
import java.util.ArrayList;
public class ListTools {
// 1. Save index 0 in a temp variable, then use get() and set() with size() - 1
public static void swapFirstAndLast(ArrayList<Integer> nums) {
}
// 2. Box target so Java calls remove(Object obj), not remove(int index)
public static void removeFirstOccurrence(ArrayList<Integer> nums, int target) {
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(1); nums.add(7); nums.add(3); nums.add(7);
swapFirstAndLast(nums);
System.out.println(nums); // expect [7, 7, 3, 1]
removeFirstOccurrence(nums, 7);
System.out.println(nums); // expect [7, 3, 1]
}
}
Code Runner Challenge
Fill in the blanks, then run it
View IPYNB Source
// CODE_RUNNER: Replace the sample values with your own, then run it
// Practice #1 - Try it yourself first!
import java.util.ArrayList;
public class MyPlaylist {
public static void main(String[] args) {
// Try writing your own code here first!
// Sample answer:
ArrayList<String> songs = new ArrayList<String>();
songs.add("Golden Hour");
songs.add("Levitating");
songs.add("Sunflower");
songs.add(0, "Espresso");
songs.set(songs.size() - 1, "Heat Waves");
songs.remove("Levitating");
System.out.println(songs + ", " + songs.size() + " songs");
}
}
MyPlaylist.main(null);
MCQ Check
4 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your notes.
Homework Hack
Task: Write two list tools. swapFirstAndLast swaps the first and last values in place using get, set, and a temporary variable. removeFirstOccurrence removes the first element equal to target using remove(Object obj).
Solution Skeleton:
// CODE_RUNNER: Fill in the blanks, then run it
import java.util.ArrayList;
public class ListTools {
// 1. Save index 0 in a temp variable, then use get() and set() with size() - 1
public static void swapFirstAndLast(ArrayList<Integer> nums) {
}
// 2. Box target so Java calls remove(Object obj), not remove(int index)
public static void removeFirstOccurrence(ArrayList<Integer> nums, int target) {
}
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(1); nums.add(7); nums.add(3); nums.add(7);
swapFirstAndLast(nums);
System.out.println(nums); // expect [7, 7, 3, 1]
removeFirstOccurrence(nums, 7);
System.out.println(nums); // expect [7, 3, 1]
}
}
ListTools.main(null);
References
College Board. (2025). AP Computer Science A course and exam description (Effective fall 2025). Topic 4.8 ArrayList Methods, pp. 117-118
Runestone Academy. (n.d.). Topic 4.8: ArrayList and its methods. In CSAwesome2.