ArrayList Introduction (Bonus)

Unit 4 · Bonus Lesson · ArrayList

ArrayList Introduction

🕑 35–45 min · 10 Practice Questions · New Syntax · Arrays vs. ArrayList

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 — requires import 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 use arr[i]; ArrayLists use list.get(i).

Practice Questions

MCQ 1
Which declaration correctly creates an ArrayList that holds String objects?
Predict before reading the options.
A
String[] list = new ArrayList();
B
ArrayList list = new ArrayList();
C
ArrayList list = new ArrayList();
D
ArrayList list = new ArrayList();
C. A declares as a String array — wrong type. B uses a raw ArrayList with no generic type — not best practice and won't satisfy the AP exam. D uses lowercase string — Java type names are case-sensitive, and the String class starts with a capital S.
MCQ 2
Which of the following will NOT compile because it uses a primitive type as the ArrayList generic?
A
ArrayList nums = new ArrayList();
B
ArrayList nums = new ArrayList();
C
ArrayList vals = new ArrayList();
D
ArrayList words = new ArrayList();
B. int is a primitive — it cannot be used as a generic type. Use Integer instead. A, C, and D all use valid object types.
MCQ 3
What is the correct way to get the number of elements currently in an ArrayList named list?
A list.length
B list.length()
C list.count()
D list.size()
D. 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.
MCQ 4
A programmer writes ArrayList scores = new ArrayList(); then calls scores.add(95). What happens to the value 95?
Predict before reading the options.
A It is autoboxed from int to Integer and added to the list.
B It causes a compile error because 95 is a primitive.
C It is stored as a String.
D It is stored as a double automatically.
A. Java's autoboxing silently converts the primitive int value 95 to an Integer object before adding it. No explicit conversion needed — the compiler handles it.
MCQ 5
Which situation best justifies using an ArrayList instead of an array?
A Storing the 12 months of the year by name
B Storing a fixed grid of 10×10 integers
C Building a list of students who scored above 90, where the count isn't known in advance
D Storing exactly 7 days of temperature readings
C. When the number of qualifying elements isn't known upfront, ArrayList is the right choice — it grows as elements are added. A, B, and D all have known fixed sizes, making arrays appropriate.
Tier 3 · AP Mastery

Mastery: ArrayList Introduction

MCQ 6
Which import statement is required to use ArrayList?
A import java.lang.ArrayList;
B import java.util.ArrayList;
C import ArrayList;
D No import needed — ArrayList is built in.
B. ArrayList is in the 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.
MCQ 7
After executing the following, what is the size of list?
ArrayList list = new ArrayList();
list.add("alpha");
list.add("beta");
list.add("gamma");
Predict before reading the options.
A 0
B 2
C 4
D 3
D. Each add call appends one element. Three calls → size is 3. The list starts empty (size 0) and grows with each add.
MCQ 8
A student writes the following. What is wrong?
ArrayList nums = new ArrayList();
nums.add(10);
nums.add(20);
System.out.println(nums.length);
A length is not a valid ArrayList member — should be size().
B ArrayList cannot hold Integer objects.
C add requires explicit new Integer() calls.
D There is nothing wrong — this code works correctly.
A. 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.
MCQ 9
Which correctly declares an ArrayList to store Student objects?
A
Student[] roster = new ArrayList();
B
ArrayList roster = new ArrayList();
C
ArrayList roster = new ArrayList();
D
ArrayList roster = new ArrayList();
C. A declares as a Student array — type mismatch. B uses a raw ArrayList with no generic type. D uses lowercase student — class names in Java are capitalized by convention and the compiler treats them as different identifiers.
MCQ 10
Which statement about arrays and ArrayLists is accurate?
A Both arrays and ArrayLists can store primitive types directly.
B Arrays have a fixed size; ArrayLists can grow and shrink dynamically.
C ArrayLists are faster than arrays for all operations.
D ArrayLists do not require an import statement.
B. This is the core distinction. A is wrong — ArrayLists cannot store primitives directly (use wrapper classes). C is wrong — arrays are generally faster for indexed access. D is wrong — 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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]