What is an ArrayList, how does it differ from an array, and how do you declare and create one?
Topic 7.1 Introduction to ArrayList: declare and create an ArrayList using generics, and explain how a resizable ArrayList differs from a fixed-size array.
A focused answer to AP CSA Topic 7.1, covering what an ArrayList is, the import statement, declaring and creating an ArrayList with generic type parameters, why only object (reference) types are allowed, autoboxing of wrapper types, and how an ArrayList differs from an array, with a fully worked example.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 7.1) wants you to know what an ArrayList is and how to declare and create one. An ArrayList is a resizable list from the Java library that stores objects. Unlike an array, it can grow and shrink as you add and remove elements. You declare its element type using generics - the type in angle brackets, such as ArrayList<String>. The element type must be a reference type, so primitives use their wrapper classes (Integer, Double) from Unit 2.
What an ArrayList is
An array has a fixed length set at creation. An ArrayList does not: you can keep adding elements and it grows, or remove elements and it shrinks. It is a class in the java.util package, so a program that uses it needs:
import java.util.ArrayList;
Declaring and creating with generics
ArrayList<String> names = new ArrayList<String>();
ArrayList<Integer> nums = new ArrayList<Integer>();
The type in angle brackets, <String> or <Integer>, is the generic type parameter: it tells the list what type of element it holds, so the compiler can check your code. A newly created ArrayList is empty, with size() equal to 0.
How an ArrayList differs from an array
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed at creation | Grows and shrinks |
| Length | length attribute |
size() method |
| Element access | a[i] |
list.get(i) |
| Element types | Any, including primitives | Reference types only |
These differences are why an ArrayList is the right choice when you do not know in advance how many elements you will store.
Try this
Q1. Why can you not declare ArrayList<int>? [1 point]
- Cue. An
ArrayList's element type must be a reference type;intis a primitive. Use the wrapper classIntegerinstead.
Q2. Write a statement that declares and creates an empty ArrayList of Double named temps. [2 points]
- Cue.
ArrayList<Double> temps = new ArrayList<Double>();- generic typeDouble(the wrapper) on both the declaration and thenewexpression.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Which of the following correctly declares and creates an empty `ArrayList` that will store `String` values?
(A) `ArrayList<String> list = new ArrayList<String>();`
(B) `ArrayList<int> list = new ArrayList<int>();`
(C) `String[] list = new ArrayList<String>();`
(D) `ArrayList list = new String[0];`
(E) `ArrayList<String> list = new String();`
Show worked answer →
The answer is (A).
An ArrayList is declared with a generic type parameter in angle brackets giving the element type, and created with new ArrayList<String>(). (B) is invalid because the type parameter must be a reference type, not the primitive int (you would use the wrapper Integer). (C) mixes an array type with an ArrayList. (D) assigns an array to an ArrayList variable. (E) creates a String, not an ArrayList.
Markers reward the correct generic syntax ArrayList<String> on both the declaration and the new expression, and using a reference type as the element type.
AP 2021 (style)4 marksFree response (code writing). Write a code segment that declares and creates an empty `ArrayList` of `Integer` named `scores`, then adds the three values 90, 85 and 100 to it in that order using the appropriate method. (Assume the necessary import is present.)
Show worked answer →
A 4-point question testing ArrayList creation with a wrapper element type and basic adding.
ArrayList<Integer> scores = new ArrayList<Integer>();
scores.add(90);
scores.add(85);
scores.add(100);
Point 1: declare with the generic type ArrayList<Integer> (the wrapper, not int). Point 2: create with new ArrayList<Integer>(). Point 3: call add to append each value. Point 4: add them in the stated order. The int literals 90, 85 and 100 are autoboxed to Integer automatically. Using ArrayList<int> would not compile.
Related dot points
- Topic 2.8 Wrapper Classes: Integer and Double: use the Integer and Double wrapper classes, including autoboxing and unboxing, the MIN_VALUE and MAX_VALUE constants, and parsing methods.
A focused answer to AP CSA Topic 2.8, covering why wrapper classes exist, creating Integer and Double objects, autoboxing and unboxing, Integer.MIN_VALUE and MAX_VALUE, parseInt and parseDouble, and the == versus equals trap for wrappers, with a worked trace.
- Topic 6.1 Array Creation and Access: declare and initialise a one-dimensional array, access elements by index from 0 to length minus 1, and use the length attribute.
A focused answer to AP CSA Topic 6.1, covering how to declare and create a one-dimensional array, default element values, the length attribute, accessing and modifying elements by index, the valid index range, and ArrayIndexOutOfBoundsException, with a fully worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)