Introduction to
Using Data Sets
Learn how to store, traverse, and analyze collections of data in Java using arrays and ArrayLists: the foundation for solving real AP CSA problems.
What Is a Data Set?
A data set is a structured collection of related values stored together so you can process them as a group. In AP Computer Science A, the two primary structures for holding data sets are arrays and ArrayLists.
Why Data Sets Matter
Almost every AP CSA free-response question involves processing a collection of data. Understanding data sets lets you find sums, averages, extremes, filter results, and transform values: skills tested in every exam section.
Declaring an Array
An array in Java is a fixed-size container. Once created, its length cannot change. Each element is accessed by a zero-based index.
Declaring an ArrayList
An ArrayList is a resizable list from java.util. It stores objects (wrapper types for primitives) and provides methods for adding, removing, and accessing elements.
Array vs ArrayList
| Array | ArrayList | |
|---|---|---|
| Size | Fixed at creation | Dynamic (grows/shrinks) |
| Stores | Primitives or Objects | Objects only |
| Access | arr[i] | list.get(i) |
| Length | .length | .size() |
| Add element | Not possible | .add(val) |
| Remove element | Not built-in | .remove(i) |
Traversing with a For Loop
The most common way to process every element in a data set is a standard for loop. The loop variable serves as the index into the collection.
The Enhanced For Loop
When you don't need the index and simply want to visit every element, Java's for-each loop is cleaner and less error-prone.
Step-by-Step Data Set Processing
Example: Finding the Average
Given an array of integers, compute the average value. A classic accumulator pattern.
Example: Counting Matches
Count how many students scored above 85 in an ArrayList. This uses a conditional accumulator pattern.
Example: Removing Elements Safely
Remove all negative values from an ArrayList. The key is to traverse backwards so removing an element doesn't skip the next one.
Essential Traversal Patterns
The Min/Max Pattern
Finding the minimum or maximum requires initializing to the first element (not 0 or Integer.MAX_VALUE in most AP cases) and comparing every element.
Common Mistakes
Operations Summary
| Operation | Array | ArrayList |
|---|---|---|
| Create | new int[n] | new ArrayList<>() |
| Read element | arr[i] | list.get(i) |
| Set element | arr[i] = v | list.set(i, v) |
| Length / size | arr.length | list.size() |
| Add to end | N/A | list.add(v) |
| Remove at index | N/A | list.remove(i) |
| For-each loop | for (int x : arr) | for (Type x : list) |
Question 1 of 5
Given int[] vals = {4, 7, 2, 9};, what is the value of vals[2]?
Question 2 of 5
Which method returns the number of elements in an ArrayList?
Question 3 of 5
Consider the following code:
int[] arr = {10, 20, 30, 40, 50};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 25) {
result += arr[i];
}
}
What is the value of result after the loop completes?
Question 4 of 5
Why should you traverse an ArrayList backwards when removing elements?
Question 5 of 5
Consider the following method:
public static double mystery(int[] a) {
int s = 0;
for (int v : a) {
s += v;
}
return s / a.length;
}
If called with {3, 4}, what does the method return?