01 / 21

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.

AP Computer Science A

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.

Array
Fixed-size, indexed collection of elements of the same type. Declared with brackets.
ArrayList
Resizable, indexed collection using the java.util package. Stores objects, not primitives.

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.

Raw Data Traverse & Process Results [85, 92, 78, 90, 88] sum / count / filter avg = 86.6

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.

// Declare and initialize an array of test scores int[] scores = {85, 92, 78, 90, 88}; // Or declare with a fixed size, then fill later int[] data = new int[100]; data[0] = 42;

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.

import java.util.ArrayList; // Create an ArrayList of Strings ArrayList<String> names = new ArrayList<String>(); names.add("Alice"); names.add("Bob"); // Access by index String first = names.get(0); // "Alice" // Size (with parentheses!) int count = names.size(); // 2

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.

// Sum all scores in an array int sum = 0; for (int i = 0; i < scores.length; i++) { sum += scores[i]; } double average = (double) sum / scores.length;
Index: Value: 0 85 1 92 2 78 3 90 4 88

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.

// Array: find the maximum score int max = scores[0]; for (int s : scores) { if (s > max) { max = s; } } // ArrayList: print all names for (String name : names) { System.out.println(name); }

Step-by-Step Data Set Processing

1
Identify the collection. Determine whether you're working with an array or an ArrayList and note the element type.
2
Choose a traversal. Use a standard for loop if you need the index; use enhanced for loop if you just need each value.
3
Initialize accumulators. Before the loop, set up variables for sum, count, max, min, or any result you need.
4
Process inside the loop. Update your accumulators or build a new collection based on each element.
5
Return or use the result. After the loop, compute any final values (e.g., average = sum / count) and return or print them.

Example: Finding the Average

Given an array of integers, compute the average value. A classic accumulator pattern.

public static double average(int[] data) { int sum = 0; for (int val : data) { sum += val; } return (double) sum / data.length; }

Example: Counting Matches

Count how many students scored above 85 in an ArrayList. This uses a conditional accumulator pattern.

public static int countAbove(ArrayList<Integer> scores, int threshold) { int count = 0; for (int s : scores) { if (s > threshold) { count++; } } return count; }

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.

public static void removeNegatives(ArrayList<Integer> list) { for (int i = list.size() - 1; i >= 0; i--) { if (list.get(i) < 0) { list.remove(i); } } }
Before: 3 -1 7 -4 5 After: 3 7 5

Essential Traversal Patterns

Accumulator
Sum, count, or combine values into a single result. Initialize before the loop, update inside.
Search
Find a specific value or the first element meeting a condition. Return early when found.
Transform
Build a new collection by applying an operation to each element of the original.

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.

public static int findMin(int[] arr) { int min = arr[0]; for (int i = 1; i < arr.length; i++) { if (arr[i] < min) { min = arr[i]; } } return min; }

Common Mistakes

✕ Off-by-One Error
Using <= instead of < in the loop condition causes an ArrayIndexOutOfBoundsException at the last index + 1.
✕ .length vs .size()
Calling .length() on an array or .length on an ArrayList. Arrays use .length; ArrayLists use .size().
✕ Forward Remove
Removing elements while traversing forward causes index shifting: you skip the element right after the removed one.

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

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

If called with {3, 4}, what does the method return?