Lesson 4.8: ArrayList Methods

Unit 4 · Lesson 4.8 · ArrayList

Lesson 4.8: ArrayList Methods

🕑 35–45 min · 10 Practice Questions · AP Reference Sheet · Method Syntax

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

MCQ 1
What is printed?
ArrayList list = new ArrayList();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list.get(1));
Predict before reading the options.
A 10
B 30
C 20
D 3
C. Index 0=10, index 1=20, index 2=30. get(1) returns 20. ArrayLists are 0-indexed just like arrays.
MCQ 2
What does the list contain after this code runs?
ArrayList list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
Trace the insert carefully.
A ["A", "B", "X", "C"]
B ["A", "X", "B", "C"]
C ["X", "A", "B", "C"]
D ["A", "B", "C", "X"]
B. 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.
MCQ 3
What is printed?
ArrayList list = new ArrayList();
list.add("red");
list.add("green");
list.add("blue");
list.remove(0);
System.out.println(list.get(0));
Trace the removal and shift before selecting.
A red
B blue
C null
D green
D. remove(0) removes "red" and shifts everything left. The list becomes ["green", "blue"]. get(0) now returns "green".
MCQ 4
What does set return?
ArrayList list = new ArrayList();
list.add("cat");
list.add("dog");
String old = list.set(0, "bird");
System.out.println(old);
A cat
B bird
C dog
D null
A. 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.
MCQ 5
What is printed?
ArrayList list = 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));
Trace every operation before selecting.
A 3 10
B 4 7
C 3 7
D 4 10
C. Step by step: add 5,10,15 → [5,10,15]. add(1,7) → [5,7,10,15] (size 4). remove(2) removes index 2 (value 10) → [5,7,15] (size 3). size()=3, get(1)=7. Output: "3 7".
Tier 3 · AP Mastery

Mastery: ArrayList Methods

MCQ 6
What is printed?
ArrayList list = 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);
Trace every step before selecting.
A [1, 2, 9, 3, 4]
B [1, 9, 3, 4]
C [9, 1, 3, 4]
D [1, 2, 3, 9, 4]
B. Start: [1,2,3,4]. remove(1) removes value at index 1 (value 2) → [1,3,4]. add(1,9) inserts 9 at index 1 → [1,9,3,4]. That's the final list.
MCQ 7
Which code correctly replaces the last element of list with the value 99?
Predict before reading the options.
A
list.set(list.size(), 99);
B
list.add(list.size(), 99);
C
list.set(list.size() + 1, 99);
D
list.set(list.size() - 1, 99);
D. The last valid index is always 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.
MCQ 8
A method should move the first element of an ArrayList to the end. Which implementation is correct?
Predict before reading the options.
A
String first = list.remove(0);
list.add(first);
B
list.add(list.get(0));
list.remove(0);
C
list.set(list.size() - 1, list.get(0));
D
list.add(0, list.remove(list.size() - 1));
A. Remove the first element (and save it), then append it to the end. B actually works too — it appends a copy first, then removes index 0 — but has a subtle ordering issue if the list has one element. C only overwrites the last element without removing the first. D moves the last element to the front, which is the opposite operation.
MCQ 9
What is printed?
ArrayList list = 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));
Evaluate the expression before tracing.
A 20
B 30
C 40
D 60
C. 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.
MCQ 10
After running this code, what does list contain?
ArrayList list = 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));
Trace every step — two operations interact.
A ["X", "W", "Y"]
B ["X", "W", "Y"]
C ["W", "X", "Y"]
D ["X", "W", "X", "Y"]
B. Start: [W,X,Y,Z]. remove(size()-1) = remove(3) removes "Z" → [W,X,Y]. Then: list.remove(1) removes "X" (returns "X") → [W,Y]. add(0, "X") inserts "X" at index 0 → [X,W,Y]. Note: Java evaluates the argument 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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]