4.02 Introduction to Using Data Sets
Work with data sets by identifying variables, interpreting records, and using data to answer meaningful questions.
Introduction to Using Data Sets
Learning Objective (4.2.A): Represent collections of related data using arrays.
Key Concepts
What is an array? An object that stores a fixed number of values of the same type, all accessible through a single variable name. (EK 4.2.A.1)
Three things every array has:
- A type — every value stored must be the same type (all
int, allString, etc.). - A fixed length — set when the array is created and never changes. If you need more room, you build a new array.
- An index for every value — indices start at
0, not1, and run throughlength - 1.
int[] scores = {85, 92, 78, 90};
// index: 0 1 2 3
// scores.length is 4, but valid indices only go up to 3
Arrays vs. Separate Variables
- Without an array, storing four test scores means four separate variables:
score1,score2,score3,score4— clunky, and impossible to loop over. - An array lets you store all of them under one name and reach any value with
scores[i], whereican be a variable — which is what makes loops and arrays so powerful together. (EK 4.2.A.2) - Anything you could do with a pile of separate variables, an array can do with far less code, and it scales — the same loop that processes 4 scores works for 400.
Building an Intuition: The Numbered Shelf
A useful way to picture an array is a shelf of identical cubbies, numbered left to right starting at 0. The shelf itself has a fixed number of slots — you can’t wedge in an extra one — and every slot holds the same kind of item. To find something, you don’t search the whole shelf; you go straight to its numbered slot. That’s exactly how scores[2] works: Java doesn’t search for the third score, it jumps directly to index 2.
This is also why the first slot is labeled 0 rather than 1 — the index tells Java how many slots to skip from the start of the shelf, and skipping zero slots gets you the first one.
Indexing and Bounds
- Valid indices for an array of length
nare0throughn - 1. (EK 4.2.A.3) array.lengthis a field, not a method — no parentheses:scores.length, neverscores.length().- Reaching for an index that doesn’t exist (
scores[4]on a length-4 array, or any negative index) throws anArrayIndexOutOfBoundsExceptionat runtime — Java won’t catch this for you at compile time.
The point of the next snippet is to make that failure concrete rather than abstract: scores only has slots 0–3, so asking for slot 4 is asking Java to read memory it never reserved.
int[] scores = {85, 92, 78, 90};
System.out.println(scores[4]); // ArrayIndexOutOfBoundsException — valid indices are 0-3
⚠️ Exam Scope Note
You will be expected to read, trace, and write basic array declaration and access code on the AP exam. You should be able to:
- Declare and initialize an array with literal values
- Access and modify a value at a given index
- Determine
array.lengthfor a given array - Predict when code will throw an
ArrayIndexOutOfBoundsException
Example to Practice Tracing
Before tracing, notice what the method is doing conceptually: it walks every index in order and prints a transformed value, which is the shape almost every array-traversal problem on the exam takes. Getting comfortable with this pattern here pays off in topics 4.4 and 4.5.
Trace printDoubled(scores):
public static void printDoubled(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] * 2);
}
}
public static void main(String[] args) {
int[] scores = {85, 92, 78, 90};
printDoubled(scores);
}
Popcorn Hack 1
Trace through printDoubled(scores) by hand. What gets printed, line by line, and why does the loop stop where it does?
Click to reveal the trace
i = 0→scores[0] * 2→85 * 2→ prints170i = 1→scores[1] * 2→92 * 2→ prints184i = 2→scores[2] * 2→78 * 2→ prints156i = 3→scores[3] * 2→90 * 2→ prints180i = 4→ loop conditioni < scores.lengthfails (4 < 4is false) → loop ends
The loop stops because scores.length is 4, and the last valid index is 3 — once i reaches 4, the condition i < arr.length is no longer true.
Popcorn Hack 2
The code below has a bug in the loop condition — it reaches one index past the end of the array. This is deliberately the single most common exam mistake on this topic, so find the bug and explain exactly what error it would throw and why, before checking below.
public static void printDoubled(int[] arr) {
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i] * 2);
}
}
Click to reveal the fix
The condition should be i < arr.length, not i <= arr.length. As written, when i equals arr.length (the first invalid index), arr[i] tries to access a slot that was never built, and Java throws an ArrayIndexOutOfBoundsException.
public static void printDoubled(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] * 2);
}
}
Popcorn Hack 3
Given int[] nums = {4, 8, 15, 16, 23, 42};, what does nums[nums.length - 1] return, and why does subtracting 1 matter here?
Click to reveal the answer
It returns 42, the last element. nums.length is 6, but valid indices only go up to 5 — subtracting 1 converts “how many elements exist” into “the index of the last one,” since indexing starts at 0 instead of 1.
⚠️ Watch Out
- Confusing
lengthwithlength()— arrays use a field (arr.length), whileStringandArrayListuse a method (str.length(),list.size()). Mixing these up is one of the most common syntax errors on this topic. - Off-by-one errors — using
<=instead of<against.lengthin a loop condition reaches one index too far and throwsArrayIndexOutOfBoundsException. - Assuming index 1 is the first element — it isn’t. Index 0 is always the first element in Java.
- Forgetting the array’s length is fixed — there’s no way to “add” a slot to an existing array. Growing a collection means creating a new, larger array (or, later in the course, using an
ArrayListinstead).
References
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025]. Unit 4: Data Collections (p. 108). https://apcentral.collegeboard.org/media/pdf/ap-computer-science-a-course-and-exam-description-effective-fall-2025.pdf