01
Overview

Find an object, then count related ones.

Two-step problems where the result of a lookup drives a counting traversal across the rest of the list.

Cross-object counting takes one object as a reference point and asks "how many other objects share some property with this one?" The first step is a lookup — find the target object. The second step is a count — traverse the list and tally objects related to the target.

Common forms: how many students share a department with the target student? How many products cost more than the named product? How many books were published in the same year as the given book?

On the AP exam, this combined pattern appears in object-list FRQs where one element is named and the rest must be analyzed in reference to it.

02
Core Concept

Look up the reference; compare every other element to it.

A lookup helper plus a counting loop.

The template

public int countRelated(ArrayList<Type> list, String targetKey) {
    // Step 1: lookup the reference object
    Type target = findByKey(list, targetKey);
    if (target == null) return 0;

    // Step 2: count related objects
    int count = 0;
    for (Type other : list) {
        if (other != target && isRelated(other, target)) {
            count++;
        }
    }
    return count;
}

The other != target guard prevents counting the target itself when "related" includes the target's own properties.

Lookup helper

private Student findByName(ArrayList<Student> list, String name) {
    for (Student s : list) {
        if (s.getName().equals(name)) {
            return s;
        }
    }
    return null;
}

Returning null when not found is the standard signal. The caller must guard against it.

Concrete example

Count how many students share a department with the student named targetName:

public int sameDepartmentCount(ArrayList<Student> students, String targetName) {
    Student target = findByName(students, targetName);
    if (target == null) return 0;

    int count = 0;
    for (Student s : students) {
        if (s != target && s.getDepartment().equals(target.getDepartment())) {
            count++;
        }
    }
    return count;
}

Reference vs content comparison

s != target compares references — true unless both variables point to the very same object. That's what we want here, since the target object is uniquely the one returned by lookup.

.equals() on the department field compares contents — the right tool for matching String values across objects.

03
Key Vocabulary

Lookup-and-count vocabulary.

Used in reference-driven FRQ problems.

Vocabulary
  • Lookup — finding the reference object by some key.
  • Reference object — the target used to compare against.
  • Cross-object property — a value shared between two objects.
  • Self-exclusion — skipping the target when counting others.
  • Reference equality — same object in memory (==).
  • Content equality — same field values (.equals()).
04
AP Exam Focus

How cross-object counting is tested.

Self-exclusion and null-target handling are the most-graded points.

Exam Focus

What FRQ graders check:

  • Null target handling. If the lookup returns nothing, return 0 (or whatever the prompt specifies). Don't crash.
  • Self-exclusion. The target itself shouldn't usually be counted as related to itself.
  • Correct comparison. .equals() for content, == or != for references.
  • Right field used. Compare the accessor named in the prompt, not a similar one.

MCQ patterns to expect: tracing the counted total for a given target; identifying what happens when the target doesn't exist.

05
Worked Example

Count books by the same author.

Lookup by title; compare authors across the list.

Worked Example AP-style reasoning

Setup. A Book class has getTitle() and getAuthor(). Given ArrayList<Book> library and a target title, return the number of other books by the same author. Return 0 if the title isn't found.

public int otherBooksByAuthor(ArrayList<Book> library, String title) {
    Book target = null;
    for (Book b : library) {
        if (b.getTitle().equals(title)) {
            target = b;
        }
    }
    if (target == null) return 0;

    int count = 0;
    for (Book b : library) {
        if (b != target && b.getAuthor().equals(target.getAuthor())) {
            count++;
        }
    }
    return count;
}

Trace. Library has five books with (title, author) pairs:

  • ("A", "Orwell"), ("B", "Atwood"), ("C", "Orwell"), ("D", "Orwell"), ("E", "Atwood").

Call with target title "C". Lookup finds the book at index 2 (author Orwell).

Counting loop: skip the target itself; count Orwell matches. Indices 0 and 3 both have author Orwell → count = 2.

Answer. 2.

What if the title were "Z"? Lookup never finds a match; target stays null; method returns 0. No crash.

06
Common Mistakes

Cross-object counting pitfalls.

Most come from missing the null guard or counting the target itself.

Watch Out
  • No null guard. Calling target.getAuthor() on a null target throws an exception.
  • Counting the target itself. The author always matches the author — so without b != target, your count is one too high.
  • Using == for String comparison. Always .equals() for content.
  • Using .equals() for the self-exclusion check. Use != on the references; .equals() compares contents.
  • Wrong field compared. Reading the prompt as "same title" when it says "same author".
  • Returning -1 when not found. Search returns -1 for indices, but cross-object counts should return 0 when the target is missing.
07
Reference Table

Lookup-and-count pattern.

Each step has its own concern.

Cross-Object Count Reference

AP Quick Reference
Step Action AP Exam Tip
1 Lookup target by key Return null if not found.
2 Guard against null Return 0 (or per spec) early.
3 Loop the full list Enhanced for is clean.
4 Skip the target itself other != target.
5 Compare relevant field .equals() for content.
6 Return the count Always a non-negative int.
08
Practice Tip

How to master cross-object counting.

Two helpers, one guard — and always test the missing-target case.

Practice Strategy

Use the three practice tools below this lesson in order:

  • MCQ Practice — for each option, check that the target is excluded from the count and the null case is handled.
  • Java Lab — implement counts for "same author", "same department", "older than the target". Test with a missing target every time.
  • FRQ Practice — write a lookup helper first; then a counting loop with self-exclusion. Two clean methods earn more credit than one messy one.

If you can split any cross-object problem into "lookup + count" without thinking, you've mastered this pattern.

09
Section · 09

Practice — attempt these now.

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

Topic Quiz 40 min 40 marks Pending

AP CSA Topic Practice: Object Lookup and Cross-Object Counting

AP-style topic practice assessment

Start
FRQ Practice 30 min 18 marks Pending

AP CSA Unit 06 Topic 10 Object Lookup and Cross-Object Counting FRQ Practice

AP-style topic practice assessment

Start