STEM-TutorLearn • Practice • ExcelBook Free DemoFree demo

Java ArrayList, explained for AP Computer Science A

An ArrayList is a resizable list of objects. It is one of the most tested topics in AP Computer Science A, and at least one free-response question every year usually involves one.

On this page
  1. What an ArrayList is
  2. Creating an ArrayList
  3. The AP subset methods
  4. Worked examples
  5. Removing elements safely
  6. Comparing Integer objects
  7. ArrayLists of your own objects
  8. ArrayList versus arrays
  9. Common FRQ patterns
  10. Common mistakes
  11. Practice questions
  12. Frequently asked questions
  13. Key takeaways
  14. Related guides

What an ArrayList is

A normal Java array has a fixed length that you choose when you create it. An ArrayList can grow and shrink as you add and remove elements, which makes it ideal when you do not know in advance how many items you will store. It lives in the java.util package, so a program needs import java.util.ArrayList; at the top. On the AP exam, imports are usually assumed.

An ArrayList can only hold objects, not primitive types. To store whole numbers you use the wrapper class Integer; for decimals, Double. Java converts between int and Integer automatically, a feature called autoboxing and unboxing.

Creating an ArrayList

ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> scores = new ArrayList<>();

The type in angle brackets is the type of element the list holds. In the second line, the empty brackets <> (the diamond) let Java infer the type. A new list starts empty, with size 0.

The AP subset methods

The AP Java Quick Reference lists the ArrayList methods you need. Know exactly what each one returns:

Indexes run from 0 to size() − 1. Using an index outside that range throws an IndexOutOfBoundsException.

Worked examples

Example 1: tracing method calls

What does the list contain after this code runs?
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add(0, "C");
list.set(2, "D");
list.remove(1);

Example 2: summing with a for-each loop

Write code that returns the sum of an ArrayList<Integer> called nums.

Example 3: finding the largest value

Write a method that returns the largest value in a non-empty ArrayList<Integer>.

Removing elements safely

Removing inside a loop is the most common ArrayList trap on the AP exam. When you remove the element at index i, every later element shifts left by one. If the loop then does i++, it skips the element that just moved into position i.

Example 4: removing all negative numbers

Remove every negative value from an ArrayList<Integer> called nums.

Comparing Integer objects

Because an ArrayList stores Integer objects, comparing two elements with == compares whether they are the same object, not whether they hold the same value. For small values Java often reuses the same objects, so == may appear to work in testing and then fail for larger numbers. Safe options are to unbox into int variables first, for example int a = list.get(0);, or to use list.get(0).equals(list.get(1)). Comparisons with < and > are fine, because they unbox automatically. The same rule applies to Strings: use equals, not ==.

ArrayLists of your own objects

Many FRQs use an ArrayList of objects from a class described in the question, such as ArrayList<Student>. Access fields through the class's methods: roster.get(i).getGrade(). Read the class description carefully, and only call methods that it lists.

ArrayList versus arrays

Mixing up length, length() and size() is a classic error: arrays use length, Strings use length(), and ArrayLists use size().

Common FRQ patterns

Free-response questions often ask you to filter a list (keep or remove elements that meet a condition), build a new list from an existing one, count elements with a property, or find an element and return its index. Read the question carefully to see whether it wants the original list changed or a new list returned. Use the class's existing methods as described, and do not rewrite methods you are told already work.

Common mistakes

Practice questions

Try each one before opening the answer.

Q1. A list holds [5, 8, 2]. What does list.set(1, 9) return, and what is the list afterwards?

Show answer
It returns 8; the list becomes [5, 9, 2].

Q2. A list holds ["x", "y", "z"]. After list.add(1, "w"), what is list.get(2)?

Show answer
The list is [x, w, y, z], so "y".

Q3. What is wrong with: for (int i = 0; i < list.size(); i++) { if (list.get(i) == 0) list.remove(i); }?

Show answer
When two zeros are next to each other, the second is skipped because elements shift left after a removal.

Q4. Write one line that adds the number 7 to the end of an ArrayList<Integer> called nums.

Show answer
nums.add(7);

Frequently asked questions

Is ArrayList on the AP Computer Science A exam?

Yes. ArrayList is a central part of the Data Collections unit in the revised AP CSA course, and it appears in both multiple-choice and free-response questions.

Do I need to know other ArrayList methods like contains or indexOf?

Use the methods listed on the current AP Java Quick Reference, which you get in the exam. Whatever is listed, you can always write searches like contains or indexOf yourself with a loop, and free-response questions often expect exactly that.

Key takeaways