Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

AP Computer Science A: Deep Dive and FRQ Mastery — Alpha School

Lesson 10: ArrayLists — Methods, Wrapper Classes and Autoboxing

An array's fixed size is a real limitation: you must know how many elements you need before you start, and you cannot add or remove one later. An ArrayList solves this — it is a resizable list that grows and shrinks as elements are added and removed, and it is one of the most used classes in the course. You create one with new ArrayList<Integer>(), add elements with add, read them with get, replace them with set, remove them with remove, and ask how many there are with size(). Unlike an array, an ArrayList knows how to manage its own capacity, so you work with the number of elements actually stored, not the size of an underlying array.

ArrayLists are generic: the type in the angle brackets says what kind of elements the list holds, as in ArrayList<String> for a list of Strings or ArrayList<Double> for a list of doubles. The generic type must be a class, not a primitive, which is why you write Integer rather than int and Double rather than double. These are the wrapper classes, one for each primitive type, and they let primitive values be treated as objects. The compiler handles the conversion automatically, a feature called autoboxing: writing list.add(5) boxes the int 5 into an Integer, and writing int x = list.get(0) unboxes the Integer back into an int.

Removing elements from an ArrayList while traversing it needs care, because removing an element shifts all the later elements down by one index. If you loop forward from index 0 and remove an element, the next element slides into the position you just visited, and the loop's increment skips over it. The standard fix is to loop backwards, from the last index down to 0, so that removals only affect indices you have already passed. This is a favourite exam trap, and the way to avoid it is to trace the indices by hand whenever a loop removes elements.

ArrayLists also differ from arrays in how you find their size and access elements: list.size() is a method with parentheses, while array.length is a field without them, and list.get(i) replaces array[i]. Confusing the two is a common compile-time error that is easy to spot once you know to look for it. In free-response answers, always use size() as the loop bound for an ArrayList, just as you use length for an array, so the loop adapts to the list's current contents rather than to a number you guessed.

Beyond add, get, set, and remove, ArrayList provides several methods that make list work shorter. The method indexOf(x) returns the position of the first occurrence of x, or −1 if it is absent, and contains(x) returns true when x is in the list. The method add(index, value) inserts a value at a position, shifting later elements right, and set(index, value) replaces the element at a position and returns the old one. An ArrayList can be traversed with an enhanced for loop, as in for (String s : names), which is the clearest way to visit every element when you do not need the index. Choose the traversal that matches the task: an index when you need positions or removals, a for-each loop when you only need values.

A worked example

Worked example: the code below builds a list of scores, removes the failing ones by looping backwards, and prints what remains. The backwards loop is the key detail: it prevents the skipping problem that a forward loop would cause.

import java.util.ArrayList;
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(72);
scores.add(45);
scores.add(91);
scores.add(58);
for (int i = scores.size() - 1; i >= 0; i--)
{
    if (scores.get(i) < 60)
    {
        scores.remove(i);
    }
}
System.out.println(scores);  // [72, 91]

The list starts as [72, 45, 91, 58]. Looping backwards, index 3 holds 58, which is removed; index 2 holds 91, which stays; index 1 holds 45, which is removed; index 0 holds 72, which stays. The result is [72, 91]. A forward loop would have removed 45 at index 1, shifting 91 into index 1, and then moved on to index 2, skipping 91 entirely. When you write removal code, always ask which direction the loop should run, and trace the indices to be sure.

Watch & Learn

One video to watch for this unit, from a trusted educator.

  • Learn Java arraylists in 9 minutes!
    A short, practical walkthrough of creating an ArrayList, adding and removing elements, and the difference between arrays and ArrayLists — good revision for this lesson.

Key Terms

ArrayList
A resizable list of objects, with methods add, get, set, remove, and size.
Wrapper class
A class such as Integer or Double that holds a primitive value as an object.
Autoboxing
Automatic conversion between a primitive value and its wrapper object.
Generic type
The type in angle brackets, such as <String>, saying what an ArrayList holds.
size()
The ArrayList method returning the number of elements currently stored.

Discussion Questions

  1. Why must an ArrayList's generic type be a class rather than a primitive?
  2. Why does removing elements in a forward loop skip some of them?
  3. When would you choose an ArrayList over an array?

Practice Questions

  1. ArrayList<String> names = new ArrayList<String>(); names.add("Ana"); What is names.size()?
  2. What does names.get(0) return, and what does names.remove(0) do?
  3. Why is ArrayList<int> not allowed in Java?
  4. What is autoboxing?
  5. A loop removes elements from an ArrayList. In which direction should it run, and why?
Show answers
  1. 1.
  2. get(0) returns "Ana"; remove(0) removes it from the list and returns it.
  3. Because a generic type must be a class, and int is a primitive; you must use ArrayList<Integer>.
  4. The automatic conversion between a primitive value and its wrapper object, such as int to Integer.
  5. Backwards, from the last index to 0, so removals do not shift unvisited elements into skipped positions.
Keep reading to complete this lesson