AP CSA Arraylist Of Objects

ArrayList of Objects in AP CSA: Complete Guide (2025-2026)

ArrayList of Objects in AP CSA combines two of the most important topics in the course — class creation (Unit 3) and data collections (Unit 4, 30–40%) — and it appears on nearly every AP exam FRQ. When an ArrayList stores instances of a class, you access each object with get(i) and then call that object's methods using dot notation: list.get(i).methodName(). This two-step access pattern, and searching or filtering by object attributes, is the core skill you must master.

💻 Code Examples — Predict First

Before running each example, write down your prediction. This is the single most effective AP exam study technique.

🤔 Predict the output before running:

Example 1: Print All Object Attributes
import java.util.ArrayList;
class Student {
    private String name;
    private int grade;
        public Student(String n, int g) {
        name = n; grade = g;
    }
        public String getName() {
        return name;
    }
        public int getGrade() {
        return grade;
    }
}
public class Main {
    public static void main(String[] args) {
        ArrayList roster = new ArrayList();
        roster.add(new Student("Alice", 92));
        roster.add(new Student("Bob", 78));
        roster.add(new Student("Carol", 88));
        for (int i = 0; i < roster.size(); i++) {
            System.out.println(roster.get(i).getName() + ": " + roster.get(i).getGrade());
        }
    }
}
Running…

Alice: 92 / Bob: 78 / Carol: 88 — roster.get(i) returns the Student object at index i. Chaining .getName() or .getGrade() calls that object's method.

🤔 Predict the output before running:

Example 2: Find Object with Max Attribute
import java.util.ArrayList;
class Student {
    private String name;
    private int grade;
        public Student(String n, int g) {
        name = n; grade = g;
    }
        public String getName() {
        return name;
    }
        public int getGrade() {
        return grade;
    }
}
public class Main {
    public static Student findTop(ArrayList roster) {
        Student top = roster.get(0);
        for (int i = 1; i < roster.size(); i++) {
            if (roster.get(i).getGrade() > top.getGrade()) {
                top = roster.get(i);
            }
        }
        return top;
    }
    public static void main(String[] args) {
        ArrayList roster = new ArrayList();
        roster.add(new Student("Alice", 92));
        roster.add(new Student("Bob", 78));
        roster.add(new Student("Carol", 99));
        System.out.println(findTop(roster).getName());
    }
}
Running…

Carol — top starts as roster.get(0). Loop compares each student's grade to top's grade, updating top when a higher grade is found. Returns the Student object itself, not just the grade.

🤔 Predict the output before running:

Example 3: Filter Into New ArrayList
import java.util.ArrayList;
class Student {
    private String name;
    private int grade;
        public Student(String n, int g) {
        name = n; grade = g;
    }
        public String getName() {
        return name;
    }
        public int getGrade() {
        return grade;
    }
}
public class Main {
    public static ArrayList passing(ArrayList roster) {
        ArrayList result = new ArrayList();
        for (Student s : roster) {
            if (s.getGrade() >= 60) {
                result.add(s);
            }
        }
        return result;
    }
    public static void main(String[] args) {
        ArrayList roster = new ArrayList();
        roster.add(new Student("Alice", 92));
        roster.add(new Student("Bob", 45));
        roster.add(new Student("Carol", 71));
        ArrayList pass = passing(roster);
        System.out.println(pass.size());
    }
}
Running…

2 — Build a new ArrayList and add only objects meeting the condition. Alice (92) and Carol (71) pass. Bob (45) does not. Result size = 2.

❌ Common Pitfalls

These are the mistakes students most often make on the AP CSA exam with ArrayList of objects AP CSA. Study them carefully.

1
⚠ Accessing an attribute without calling the getter

Instance variables are private. You cannot write roster.get(i).grade — that's a compile error. You must call the getter: roster.get(i).getGrade().

roster.get(i).grade      // COMPILE ERROR (private field)
roster.get(i).getGrade() // CORRECT
2
⚠ Forgetting to initialize the result ArrayList before the loop

When building a filtered list, create the result ArrayList BEFORE the loop: ArrayList result = new ArrayList<>();. Creating it inside the loop reinitializes it every iteration.

// BUG: result recreated every iteration
for (Student s : roster) {
    ArrayList result = new ArrayList<>();
    if (cond) {
        result.add(s);
    }
}
3
⚠ Initializing max object to null instead of get(0)

Writing Student top = null and then calling top.getGrade() inside the loop throws NPE. Initialize to get(0) and start the loop at index 1.

4
⚠ Using == to compare String attributes

To check if two Strings are equal, use .equals(), not ==. This applies when searching for an object by a String field like name.

roster.get(i).getName() == "Alice"       // Unreliable
roster.get(i).getName().equals("Alice") // Correct
🎓 AP Exam Tip

AP FRQs involving ArrayList of Objects almost always ask you to do one of three things: (1) find the object with the max/min attribute value, (2) build a new list of objects that meet a condition, or (3) modify or remove objects that meet a condition. Practice all three templates.

⚠ Watch Out!

The two-step chain list.get(i).methodName() is the most important syntax pattern in Unit 4 FRQs. If you write list.methodName() (calling the method on the list instead of the object), you'll get a compile error. Always get the object first, then call its method.

✍ Check for Understanding (8 Questions)

Your Score: 0 / 0
1. How do you access the grade of the Student at index 2 in ArrayList roster?
2. What is the correct way to compare two Student objects' names?
3. What is printed?
roster.add(new Student("A",90));
roster.add(new Student("B",80));
System.out.println(roster.get(1).getName());
4. What is wrong with this code?
Student top = null;
for(int i=0;i if(roster.get(i).getGrade()>top.getGrade())
top=roster.get(i);
5. Which pattern correctly builds a list of Students with grade >= 90?
6. After this code, what is in result?
ArrayList result = new ArrayList<>();
for(Student s:roster)
if(s.getName().startsWith("A"))
result.add(s);

roster has: Alice(90), Bob(80), Anna(75)
7. What does this method return for roster = [Student("X",60), Student("Y",90)]?
Student best = roster.get(0);
for(int i=1;i if(roster.get(i).getGrade()>best.getGrade())
best=roster.get(i);
return best.getName();
8. Why must you use a getter to access a Student's name instead of accessing the field directly?

❓ Frequently Asked Questions

How do you call a method on an object stored in an ArrayList?

Use the two-step pattern: list.get(i) retrieves the object, then chain the method call: list.get(i).methodName(). For example, roster.get(0).getName() gets the first student's name.

Why can't I access instance variables directly with list.get(i).variableName?

Instance variables are declared private in AP CSA. Private fields cannot be accessed from outside the class. You must use the public getter method: list.get(i).getVariableName().

How do I find the object with the maximum attribute in an ArrayList?

Initialize a reference variable to list.get(0). Loop from index 1 to size()-1. If the current object's attribute is greater than the reference object's attribute, update the reference. After the loop, return the reference variable.

How do I build a filtered ArrayList?

Declare an empty result ArrayList before the loop. Loop through the source list. If an element meets your condition, add it to result. Return result after the loop.

How do I compare String attributes in ArrayList objects?

Use .equals(), not ==. For example: list.get(i).getName().equals("target"). The == operator checks if two variables reference the same object in memory, not if they have the same content.

TC

Tanner Crow — AP CS Teacher & Tutor

11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.

  • 54.5% of students score 5 on AP CSA (national avg: 25.5%)
  • 1,845+ verified tutoring hours • 5.0 rating
  • Free 1-on-1 tutoring inquiry: Wyzant Profile

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

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com