Skip to main content

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>
  • size
  • add
  • get and set
  • remove

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 add and get, 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.

MethodWhat it doesReturns
size()counts the elementsthe number of elements, an int
add(obj)adds obj at the endtrue
add(index, obj)inserts obj at index, shifting elements from index on one place rightnothing (void)
get(index)looks up an elementthe element at index
set(index, obj)replaces the element at index with objthe element that was there before
remove(index)removes the element at index, shifting later elements one place leftthe 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.

  1. Example 1

    Tracing list operations

    What does this code print? (Printing an ArrayList shows 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 solution
    1. Step 1: After the first two adds: [cat, dog].
    2. Step 2: add(0, "ant") inserts at the front and shifts the others right: [ant, cat, dog].
    3. Step 3: add("eel") appends: [ant, cat, dog, eel].
    4. Step 4: set(2, "fox") replaces dog with fox and returns the old element, so old is "dog". The list is [ant, cat, fox, eel].
    5. Step 5: remove(1) removes cat and returns it, so gone is "cat". The later elements shift left: [ant, fox, eel], size 3.
    6. Step 6: words.get(1) is now "fox".

    Answer: Two lines: [ant, fox, eel] 3, then dog cat fox.

  2. 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 solution
    1. Step 1: After two adds the list is [71.5, 68.0], so size() is 2.
    2. Step 2: add(2, 74.0) inserts at index 2. That's equal to size(), which is allowed and means "at the end".
    3. Step 3: By contrast, inserting at index 3 here would throw an IndexOutOfBoundsException, because it's more than size().

    Answer: It prints [71.5, 68.0, 74.0].

Common mistakes

  • Using length or square brackets with an ArrayList. Use size(), get(i) and set(i, x).
  • Forgetting that add(index, obj) and remove(index) shift the indexes of every later element.
  • Forgetting that set and remove return 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 ArrayList of objects, so know exactly what each one returns.

Connected topics

Videos

  • AP Computer Science A - Topic 4.8 - Part 1: ArrayList Methods

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA – Unit 7: ArrayList – Lesson 2: ArrayList Methods

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • ArrayList in Java Tutorial #36

    Alex LeeWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.8 - Part 2: ArrayList Methods

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.8 - Part 3: ArrayList Methods

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Learn Java arraylists in 9 minutes! 📃

    Bro CodeWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 4.8 ArrayList Methods. Pick an answer to see if you got it, and why.

Question 1 of 4

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?

Question 2 of 4

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?

Question 3 of 4

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?

Question 4 of 4

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