ArrayList Introduction (Bonus)
ArrayList Introduction
What You'll Learn
- 4.7.A: Represent a list of data using an ArrayList.
- Explain why ArrayList exists and when to choose it over an array.
- Declare and instantiate an ArrayList using the correct import and generic type syntax.
- Understand that ArrayList can only store objects, not primitives — and how autoboxing handles this.
- Compare ArrayList and array across size flexibility, syntax, and use cases.
Key Vocabulary
| Term | Definition |
|---|---|
| ArrayList | A resizable, ordered collection class from the java.util package that can grow or shrink as elements are added or removed. |
| generic type | The type parameter in angle brackets that specifies what kind of objects an ArrayList holds: ArrayList, ArrayList. |
| autoboxing | Java's automatic conversion of a primitive (like int) to its wrapper class object (Integer) when adding to an ArrayList. |
| wrapper class | An object version of a primitive type: Integer for int, Double for double, Boolean for boolean. |
Why ArrayList Exists
Arrays are powerful but have one critical limitation: their size is fixed at creation. If you need to add a 6th element to a 5-element array, you can't — you'd have to create a brand new larger array and copy everything over.
ArrayList solves this. It's a class that manages a resizable list for you. You can add elements, remove elements, and the ArrayList handles resizing automatically behind the scenes.
📌 When to Use Each
Use an array when the number of elements is known in advance and won't change. Use an ArrayList when elements need to be added or removed dynamically — filtering results into a new list, building a collection from user input, or any situation where the final size isn't known upfront.
Declaring and Creating an ArrayList
ArrayList lives in the java.util package, so you need an import statement at the top of your file:
import java.util.ArrayList;
Then declare and instantiate:
ArrayList names = new ArrayList();
// The diamond operator (empty <>) also works in modern Java:
ArrayList names = new ArrayList<>();
The type in angle brackets is the generic type — it tells Java what kind of objects this list will hold. You must use an object type here, not a primitive.
Common ArrayList Declarations
ArrayList words = new ArrayList();
ArrayList numbers = new ArrayList();
ArrayList prices = new ArrayList();
ArrayList roster = new ArrayList();
⚠️ AP Exam Trap: No Primitives in ArrayList
ArrayList does not compile. ArrayList can only hold objects. Use the wrapper class instead: ArrayList for ints, ArrayList for doubles. Java's autoboxing then lets you add literal int values and they get converted to Integer objects automatically.
Autoboxing and Unboxing
Because ArrayList requires objects, Java automatically converts between primitives and their wrapper classes:
ArrayList nums = new ArrayList();
nums.add(42); // autoboxing: int 42 → Integer 42
int x = nums.get(0); // unboxing: Integer 42 → int 42
You write code as if you're working with int values, and Java handles the conversion silently. You don't need to call new Integer(42) explicitly.
ArrayList vs. Array — Side by Side
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed at creation | Dynamic — grows and shrinks |
| Element types | Primitives or objects | Objects only (wrapper classes for primitives) |
| Access syntax | arr[i] | list.get(i) |
| Size check | arr.length | list.size() |
| Add elements | Not possible after creation | list.add(element) |
| Remove elements | Not possible | list.remove(index) |
| Import needed | No | import java.util.ArrayList; |
⚠️ AP Exam Trap: length vs. size()
Arrays use .length (a field, no parentheses). ArrayLists use .size() (a method, with parentheses). Mixing these up is one of the most common errors on AP free-response questions. If you write list.length or arr.size(), your code won't compile.
Summary
- ArrayList is a resizable list from
java.util— requiresimport java.util.ArrayList;. - Declare with a generic type:
ArrayList,ArrayList. - ArrayList can only hold objects — use wrapper classes (
Integer,Double) for primitives. - Autoboxing converts primitives to wrapper objects automatically when adding to an ArrayList.
- Arrays use
.length; ArrayLists use.size(). Arrays usearr[i]; ArrayLists uselist.get(i).
Practice Questions
String[] list = new ArrayList();
ArrayList list = new ArrayList();
ArrayListlist = new ArrayList ();
ArrayListlist = new ArrayList ();
string — Java type names are case-sensitive, and the String class starts with a capital S.ArrayListnums = new ArrayList ();
ArrayListnums = new ArrayList ();
ArrayListvals = new ArrayList ();
ArrayListwords = new ArrayList ();
int is a primitive — it cannot be used as a generic type. Use Integer instead. A, C, and D all use valid object types.list?list.length
list.length()
list.count()
list.size()
size() is the ArrayList method for element count. length (no parentheses) is the array field. length() is the String method. count() doesn't exist in Java's ArrayList.ArrayList scores = new ArrayList(); then calls scores.add(95). What happens to the value 95?int to Integer and added to the list.double automatically.int value 95 to an Integer object before adding it. No explicit conversion needed — the compiler handles it.Mastery: ArrayList Introduction
import java.lang.ArrayList;
import java.util.ArrayList;
import ArrayList;
java.util package. java.lang is auto-imported (it contains String, Math, etc.) but ArrayList is not in it. D is wrong — unlike arrays, ArrayList requires an explicit import.list?
ArrayListlist = new ArrayList (); list.add("alpha"); list.add("beta"); list.add("gamma");
add call appends one element. Three calls → size is 3. The list starts empty (size 0) and grows with each add.ArrayListnums = new ArrayList (); nums.add(10); nums.add(20); System.out.println(nums.length);
length is not a valid ArrayList member — should be size().add requires explicit new Integer() calls.length is the array field. ArrayList uses size(). This is a compile-time error — nums.length doesn't exist. B is wrong — Integer is a valid generic type. C is wrong — autoboxing handles the conversion automatically.Student objects?Student[] roster = new ArrayList();
ArrayList roster = new ArrayList();
ArrayListroster = new ArrayList ();
ArrayListroster = new ArrayList ();
student — class names in Java are capitalized by convention and the compiler treats them as different identifiers.import java.util.ArrayList; is required.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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]