A class with an ArrayList as a field.
When the object needs to track many items dynamically, an ArrayList instance variable is the natural choice — and the class methods add, remove, and query it.
An ArrayList instance variable lets a class grow and shrink its collection at runtime. The constructor initializes it (usually empty); methods add to it, remove from it, search it, or compute over it.
The class is responsible for protecting the ArrayList from outside misuse — usually by exposing methods rather than returning the list itself.
On the AP exam, ArrayList-field FRQs appear in library, inventory, and roster designs.
Private ArrayList; public methods.
All operations go through the class.
Library class
public class Library {
private ArrayList<String> books;
public Library() {
books = new ArrayList<String>();
}
public void addBook(String title) {
books.add(title);
}
public boolean removeBook(String title) {
for (int i = 0; i < books.size(); i++) {
if (books.get(i).equals(title)) {
books.remove(i);
return true;
}
}
return false;
}
public boolean hasBook(String title) {
for (String b : books) {
if (b.equals(title)) return true;
}
return false;
}
public int getCount() { return books.size(); }
}
Why not return the ArrayList directly
If getBooks() returned books, callers could modify it directly — bypassing all the class's logic. Better to expose methods that do specific operations.
Standard operations
- add: append to the list.
- remove: find and delete.
- contains: search and report.
- count: report size.
ArrayList-field vocabulary.
Used in collection-management classes.
- ArrayList field — dynamic-size collection as an instance variable.
- Encapsulated collection — accessible only via methods.
- add / remove / get / size — core ArrayList methods.
- Wrapper method — class method that delegates to ArrayList.
- Defensive design — preventing external mutation of internal collections.
How ArrayList classes are tested.
Constructor and method signatures dominate.
- Constructor initializes the ArrayList. Forgetting causes
NullPointerException. - Use
.equals()for String search. - Backward iteration for removal. If multiple removals in one method.
- Return correct types. int for size; boolean for found-or-removed.
Trace operations on a Library.
Add, search, remove.
Setup. Library lib = new Library();
- lib.addBook("Dune"); lib.addBook("1984"); lib.addBook("Foundation");
- lib.getCount() → 3.
- lib.hasBook("1984") → true.
- lib.removeBook("Dune") → true; lib.getCount() → 2.
- lib.removeBook("Dune") → false (already removed).
- lib.hasBook("Dune") → false.
ArrayList-field pitfalls.
Most miss the constructor or break encapsulation.
- Forgetting
new ArrayList()in constructor. NullPointerException on first add. - Returning the ArrayList itself. External code can mutate it.
- Using
==for String search. Use.equals(). - Adding to a public field. Loses encapsulation.
ArrayList-field methods.
Standard operations.
ArrayList Class Reference
AP Quick Reference| Method | Purpose | Returns |
|---|---|---|
| Constructor | Init empty list | — |
| add(item) | Append | void |
| remove(item) | Find and delete | boolean |
| contains(item) | Search | boolean |
| getCount() | Size | int |
How to master ArrayList-field classes.
Always init in the constructor; expose via methods.
- MCQ Practice — verify the constructor instantiates the ArrayList.
- Java Lab — build Library, Inventory, Roster. Test add, remove, contains.
- FRQ Practice — never expose the raw ArrayList; provide specific methods instead.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 13 Topic 06 Complete Class with an ArrayList Field FRQ Practice
AP-style topic practice assessment