List Logic Slides List Logic
Mastering ArrayLists & Object Collections
The Blueprint
01: DECLARATION
1. Import
Before building, you must import the utility from the Java library.
import java.util.ArrayList;
2. Syntax
ArrayLists use Generics <E> to define what type of objects they store.
// Declaration & Initialization
ArrayList<Robot> botList = new ArrayList<Robot>();
Note: ArrayLists can only store Objects , not primitives like int or double . Use wrappers like Integer instead.
Assembling the List
02: MUTATION
.add(obj)
Appends the object to the end of the list.
.add(index, obj)
Inserts at a specific spot. Shifts everything else to the right.
// Collecting Objects
// 1. Create the objects
Robot r1 = new Robot("Atlas", 95);
Robot r2 = new Robot("Spot", 40);
// 2. Add them to the collection
botList.add(r1);
botList.add(r2);
botList.add(new Robot("Dash", 75));
The Search Pattern
03: ALGORITHMS
Finding the Extremum
To find the "best", "oldest", or "heaviest" object, we follow a standard 3-step loop pattern:
Set initial tracker
Loop through the list
Update tracker if found
// Find Robot with max power
public Robot getStrongest() {
// 1. Tracker starts at index 0
Robot strongest = botList.get(0);
// 2. Loop starting at index 1
for (int i = 1; i < botList.size(); i++) {
Robot current = botList.get(i);
// 3. Comparison logic
if (current.getPower() > strongest.getPower()) {
strongest = current;
}
}
return strongest;
}
Developer Warnings
01
The .size() Trap
ArrayLists use .size() , not .length (arrays) or .length() (Strings).
02
The Empty List Crash
Calling .get(0) on an empty list throws an IndexOutOfBoundsException . Always check if the list is empty first!
03
The Initialization Error
Declaring ArrayList<E> list; only creates a null reference. You must use new ArrayList<E>() before adding items.
Object Collector FRQs Object Collector FRQs
ArrayList Manipulation & Object Searching
NAME:
DATE:
1. The Robot Forge
You are managing a fleet of robots. Each Robot object has a name and a power level.
public class Robot {
private String name;
private int powerLevel;
public Robot(String n, int p) { name = n; powerLevel = p; }
public String getName() { return name; }
public int getPower() { return powerLevel; }
}
Part A: The Fleet Assembly
Write the method buildFleet which takes an array of Strings names and an array of integers levels of the same length, and returns an ArrayList containing Robot objects created from the arrays.
Part B: Maximum Power
Write the method getStrongest which takes an ArrayList<Robot> fleet and returns the Robot with the highest power level. If the list is empty, return null.
2. The Archive
A digital library tracks Book objects. Each book has a title and the year it was published.
public class Book {
private String title;
private int yearPublished;
public Book(String t, int y) { title = t; yearPublished = y; }
public int getYear() { return yearPublished; }
}
The Oldest Manuscript
Write the method findOldest which takes an ArrayList<Book> catalog as a parameter. The method should return the Book object with the smallest (earliest) year. You may assume catalog has at least one book.
3. Climate Collector
The Reading class stores a daily temperature recording.
public class Reading {
private double temp;
public Reading(double t) { temp = t; }
public double getTemp() { return temp; }
}
Part A: Average Temperature
Write the method calcAverage which returns the average temperature of all Reading objects in the ArrayList<Reading> data. If the list is empty, return 0.0.
Part B: Filtering Heat
Write the method getExtremeDays which takes ArrayList<Reading> data and a double threshold. It should return a new containing only the readings that are strictly the threshold.
Collector Key Teacher Guide Collector Key
Teacher Solution Guide & Scoring Notes
ANSWER KEY
1 The Robot Forge
Part A: buildFleet
public ArrayList<Robot> buildFleet(String[] names, int[] levels) {
ArrayList<Robot> fleet = new ArrayList<Robot>();
for (int i = 0; i < names.length; i++) {
fleet.add(new Robot(names[i], levels[i]));
}
return fleet;
}
Note: Ensure students use new ArrayList<Robot>() and .add().
Part B: getStrongest
public Robot getStrongest(ArrayList<Robot> fleet) {
if (fleet.size() == 0) return null;
Robot strongest = fleet.get(0);
for (int i = 1; i < fleet.size(); i++) {
if (fleet.get(i).getPower() > strongest.getPower()) {
strongest = fleet.get(i);
}
}
return strongest;
}
2 The Archive
public Book findOldest(ArrayList<Book> catalog) {
Book oldest = catalog.get(0);
for (Book b : catalog) { // Enhanced for-loop is also valid here
if (b.getYear() < oldest.getYear()) {
oldest = b;
}
}
return oldest;
}
Common Pitfall
Initializing oldestYear = 0. This will fail since no year is likely less than 0.
Best Practice
Always initialize your "best" tracker with the first element of the list.
3 Climate Collector
Part A: calcAverage
public double calcAverage(ArrayList<Reading> data) {
if (data.size() == 0) return 0.0;
double total = 0;
for (Reading r : data) {
total += r.getTemp();
}
return total / data.size();
}
Part B: getExtremeDays
public ArrayList<Reading> getExtremeDays(ArrayList<Reading> data, double threshold) {
ArrayList<Reading> filtered = new ArrayList<Reading>();
for (Reading r : data) {
if (r.getTemp() > threshold) {
filtered.add(r);
}
}
return filtered;
}
Note: Students must instantiate a new ArrayList, not modify the original.