Lesson 4.15: Using Data Sets with Arrays and ArrayLists
Lesson 4.15: Using Data Sets with Arrays and ArrayLists
What You'll Learn
- 4.15.A: Use a data set stored in an array or ArrayList to solve a problem.
- Load a data set from a file into an ArrayList of objects.
- Apply multiple algorithms to a loaded data set in sequence.
- Recognize the complete FRQ #3 workflow: file → objects → ArrayList → algorithms.
- Write methods that operate on a data set to answer real-world questions.
📌 This Lesson Is the Integration Point
Lessons 4.2 (data sets), 4.7–4.10 (ArrayList), 4.14 (file reading), and 4.5 (algorithms) all come together here. A real program reads data from a file, stores it as objects in an ArrayList, and runs algorithms on the collection to answer questions. That complete workflow is what FRQ #3 tests every year.
The Complete Data Set Workflow
Every data-driven program follows the same three phases:
- Load: Read data from a file into an ArrayList of objects.
- Store: Each row of data becomes one object; the ArrayList holds all objects.
- Process: Apply algorithms (filter, count, aggregate, search) to answer questions.
✅ Complete Example: Student Grade Analysis
Suppose students.txt has one student per line: name, then score, space-separated.
// Phase 1 & 2: Load file into ArrayList of Student objects
ArrayList roster = new ArrayList();
try {
File f = new File("students.txt");
Scanner sc = new Scanner(f);
while (sc.hasNext()) {
String name = sc.next();
int score = sc.nextInt();
roster.add(new Student(name, score));
}
sc.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
// Phase 3: Process — count passing students
int passing = 0;
for (Student s : roster) {
if (s.getScore() >= 70) passing++;
}
System.out.println("Passing: " + passing);
Applying Multiple Algorithms to One Data Set
Once loaded, the same ArrayList can be queried multiple ways without reloading:
// Average score
int total = 0;
for (Student s : roster) {
total += s.getScore();
}
double avg = (double) total / roster.size();
// Highest scorer's name
Student best = roster.get(0);
for (int i = 1; i < roster.size(); i++) {
if (roster.get(i).getScore() > best.getScore()) {
best = roster.get(i);
}
}
System.out.println("Top student: " + best.getName());
// All students above average
ArrayList aboveAvg = new ArrayList();
for (Student s : roster) {
if (s.getScore() > avg) {
aboveAvg.add(s);
}
}
What the AP Exam Tests
FRQ #3 always follows this pattern:
- A class is given (like
Student,Movie,Product) with a few instance variables and getter methods. - An ArrayList of that class is given as a parameter.
- You write 1–2 methods that process the ArrayList using the algorithms you've practiced.
The file-reading code is typically given to you — you're tested on what you do with the data after it's loaded, not on the file-reading syntax itself.
Summary
- The complete workflow: file → Scanner → objects → ArrayList → algorithms.
- Load once, query many times — the ArrayList persists in memory for all processing.
- FRQ #3 gives you the class and ArrayList; you write the algorithm methods.
- Every algorithm from Lessons 4.5 and 4.10 applies to a loaded data set.
Practice Questions
ArrayList. Which phase correctly describes loading the data?new Product(...) to add each record to the listlist.size() to count productsMovie class has getTitle(), getYear(), and getRating() (double). An ArrayList films is already loaded. Which method correctly returns the number of movies with a rating above 8.0?int count = 0;
for (Movie m : films) {
if (m.getYear() > 8.0) count++;
}
return count;
return films.size();
int count = 0;
for (Movie m : films) {
if (m.getRating() > 8.0) count++;
}
return count;
int count = 0;
for (Movie m : films) {
if (m.getRating() >= 8.0) count++;
}
return count;
getYear() instead of getRating() — wrong getter. B returns total count regardless of rating. D uses >= which includes exactly 8.0 — the problem says "above 8.0" (strictly greater). C is correct.Mastery: Using Data Sets with Arrays and ArrayLists
WeatherReading class has getDate() (String), getHigh() (int), and getLow() (int). An ArrayList readings is loaded from a file. Which method correctly returns the average of all high temperatures?int total = 0;
for (WeatherReading r : readings) {
total += r.getLow();
}
return (double) total / readings.size();
int total = 0;
for (WeatherReading r : readings) {
total += r.getHigh();
}
return total / readings.size();
int total = 0;
for (WeatherReading r : readings) {
total += r.getHigh();
}
return (double) total / readings.size();
int total = 0;
for (WeatherReading r : readings) {
total += r.getHigh() + r.getLow();
}
return (double) total / readings.size();
WeatherReading class, which method correctly returns an ArrayList of all readings where the high exceeded 90?for (WeatherReading r : readings) {
if (r.getHigh() > 90) readings.remove(r);
}
return readings;
ArrayListhot = new ArrayList (); for (WeatherReading r : readings) { if (r.getHigh() > 90) hot.add(r); } return hot;
ArrayListhot = new ArrayList (); for (WeatherReading r : readings) { if (r.getLow() > 90) hot.add(r); } return hot;
ArrayListhot = new ArrayList (); for (WeatherReading r : readings) { if (r.getHigh() > 90) hot.add(r.getHigh()); } return hot;
Transaction class has getAmount() (double) and getCategory() (String). Which method correctly returns the total amount for the "Food" category?double total = 0;
for (Transaction t : ledger) {
if (t.getCategory() == "Food") {
total += t.getAmount();
}
}
return total;
double total = 0;
for (Transaction t : ledger) {
if (t.getAmount() == "Food") {
total += t.getAmount();
}
}
return total;
double total = 0;
for (Transaction t : ledger) {
if (t.getCategory().equals("food")) {
total += t.getAmount();
}
}
return total;
double total = 0;
for (Transaction t : ledger) {
if (t.getCategory().equals("Food")) {
total += t.getAmount();
}
}
return total;
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]