01
Overview

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.

02
Core Concept

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.
03
Key Vocabulary

ArrayList-field vocabulary.

Used in collection-management classes.

Vocabulary
  • 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.
04
AP Exam Focus

How ArrayList classes are tested.

Constructor and method signatures dominate.

Exam Focus
  • 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.
05
Worked Example

Trace operations on a Library.

Add, search, remove.

Worked Example AP-style reasoning

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.
06
Common Mistakes

ArrayList-field pitfalls.

Most miss the constructor or break encapsulation.

Watch Out
  • 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.
07
Reference Table

ArrayList-field methods.

Standard operations.

ArrayList Class Reference

AP Quick Reference
MethodPurposeReturns
ConstructorInit empty list
add(item)Appendvoid
remove(item)Find and deleteboolean
contains(item)Searchboolean
getCount()Sizeint
08
Practice Tip

How to master ArrayList-field classes.

Always init in the constructor; expose via methods.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 19 marks Pending

AP CSA Unit 13 Topic 06 Complete Class with an ArrayList Field FRQ Practice

AP-style topic practice assessment

Start