4.02 Introduction to Using Data Sets
Work with data sets by identifying variables, interpreting records, and using data to answer meaningful questions.
- Reference Guide
- College Board Requirements
- Lesson Plan
- Tech Talk
- Code Runner Challenge
- Code Examples
- A. Separate variables vs. an array. The
ScoreComparisonclass stores the same four scores both ways - Code Runner Challenge
- Code Runner Challenge
- B. Bounds checking. The
BoundsCheckclass shows a valid index next to an invalid one - Code Runner Challenge
- Code Runner Challenge
- C. Tracing a loop. The
DoubledTraceclass walks every index and prints a changed value - Code Runner Challenge
- A. Separate variables vs. an array. The
- Hacks & Practice Tasks
- References
Reference Guide
Key Topics
| Term | Definition | Example |
|---|---|---|
| Array | An object that stores a fixed number of values of the same type under one name. | int[] scores = {85, 92, 78, 90}; |
| Element | One value stored in the array. | 92 is an element of scores |
| Index | The position of an element. Indices start at 0. | scores[1] is 92 |
| Length | How many elements the array holds. Set at creation, never changes. | scores.length is 4 |
| Last valid index | Always one less than the length. | scores[scores.length - 1] is 90 |
| ArrayIndexOutOfBoundsException | Runtime error when an index is negative or is length or more. |
scores[4] |
- An array is a reference type: the variable holds the address of the array object, not the values themselves.
lengthis a field, not a method, so it never has parentheses.
Array Syntax
| Task | Syntax | Example | Notes |
|---|---|---|---|
| Declare | type[] name; |
int[] scores; |
No array exists yet |
| Create with a size | new type[n] |
scores = new int[4]; |
Every slot gets a default value |
| Initializer list | {v1, v2, ...} |
int[] scores = {85, 92, 78, 90}; |
Length is the number of values |
| Access | name[i] |
scores[2] |
Returns 78 |
| Modify | name[i] = value; |
scores[2] = 80; |
Replaces the old value |
| Length | name.length |
scores.length |
Field, no () |
Picking the Right Tool
- A few unrelated values (a name, an age) → separate variables
- Many values of the same kind that get processed the same way → an array
- The first value →
arr[0] - The last value →
arr[arr.length - 1] - Every value → a loop from
0whilei < arr.length - A collection that needs to grow → build a new, larger array (or use
ArrayListlater in the unit)
College Board Requirements
AP CSA Unit 4, Topic 4.2 Introduction to Using Data Sets. Learning Objective 4.2.A: Represent collections of related data using arrays. Summarized from the course and exam description (College Board, 2025, p. 108):
- 4.2.A.1 An array stores a fixed number of values of the same type, all reached through one variable name.
- 4.2.A.2 Arrays let a program store related values together and reach any of them by index, which is what makes loops over data possible.
- 4.2.A.3 Valid indices run from
0tolength - 1; any other index causes anArrayIndexOutOfBoundsException.
This lesson only covers creating and accessing single values. Full traversals are covered in 4.4, and ArrayList comes later in the unit.
Lesson Plan
Learning Objective: Represent a collection of related data with an array, and access, modify, and measure it correctly.
Success Criteria: You can declare an array with an initializer list, reach the first and last elements using length, and predict when an index is out of bounds.
Tech Talk
An array is a shelf of identical cubbies numbered left to right starting at 0. The shelf has a fixed number of slots, and every slot holds the same kind of item. To find something, Java does not search the shelf; it jumps straight to the numbered slot.
The index tells Java how many slots to skip from the start. Skipping zero slots gets you the first one, which is why the first index is 0 and the last is length - 1.
Run it, then change one score and predict the new output
public class ScoreShelf {
public static void main(String[] args) {
int[] scores = {85, 92, 78, 90};
System.out.println("Number of scores: " + scores.length);
System.out.println("First score (index 0): " + scores[0]);
System.out.println("Third score (index 2): " + scores[2]);
System.out.println("Last score (index " + (scores.length - 1) + "): " + scores[scores.length - 1]);
scores[2] = 80;
System.out.println("Third score after update: " + scores[2]);
}
}
Code Runner Challenge
Run it, then change one score and predict the new output
View IPYNB Source
// CODE_RUNNER: Run it, then change one score and predict the new output
public class ScoreShelf {
public static void main(String[] args) {
int[] scores = {85, 92, 78, 90};
System.out.println("Number of scores: " + scores.length);
System.out.println("First score (index 0): " + scores[0]);
System.out.println("Third score (index 2): " + scores[2]);
System.out.println("Last score (index " + (scores.length - 1) + "): " + scores[scores.length - 1]);
scores[2] = 80;
System.out.println("Third score after update: " + scores[2]);
}
}
ScoreShelf.main(null);
Code Examples
A. Separate variables vs. an array. The ScoreComparison class stores the same four scores both ways
Three steps get mixed up a lot:
Run it, then add a fifth score to both versions and count how many lines each one needed
public class ScoreComparison {
public static void main(String[] args) {
// Without an array: four names, no way to loop
int score1 = 85;
int score2 = 92;
int score3 = 78;
int score4 = 90;
System.out.println("Separate: " + score1 + " " + score2 + " " + score3 + " " + score4);
// With an array: one name, index can be a variable
int[] scores = {85, 92, 78, 90};
for (int i = 0; i < scores.length; i++) {
System.out.println("scores[" + i + "] = " + scores[i]);
}
}
}
Code Runner Challenge
Run it, then add a fifth score to both versions and count how many lines each one needed
View IPYNB Source
int[] scores; // declaration: names the variable, no array yet
scores = new int[4]; // creation: builds 4 slots, all 0
int[] quiz = {85, 92, 78, 90}; // initializer list: declare, create, and fill at once
Run it, then try scores[-1] inside the try block and see what happens
public class BoundsCheck {
public static void main(String[] args) {
int[] scores = {85, 92, 78, 90};
System.out.println("Valid indices: 0 to " + (scores.length - 1));
System.out.println("scores[3] = " + scores[3]);
try {
System.out.println(scores[4]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("scores[4] failed: " + e.getMessage());
}
}
}
Code Runner Challenge
Run it, then try scores[-1] inside the try block and see what happens
View IPYNB Source
// CODE_RUNNER: Run it, then add a fifth score to both versions and count how many lines each one needed
public class ScoreComparison {
public static void main(String[] args) {
// Without an array: four names, no way to loop
int score1 = 85;
int score2 = 92;
int score3 = 78;
int score4 = 90;
System.out.println("Separate: " + score1 + " " + score2 + " " + score3 + " " + score4);
// With an array: one name, index can be a variable
int[] scores = {85, 92, 78, 90};
for (int i = 0; i < scores.length; i++) {
System.out.println("scores[" + i + "] = " + scores[i]);
}
}
}
ScoreComparison.main(null);
Terminology:
The index inside the brackets can be any int expression, including a variable like i. That is what lets one loop handle 4 scores or 400.
B. Bounds checking. The BoundsCheck class shows a valid index next to an invalid one
Predict every line before running, then change < to <= and explain the new output
public class DoubledTrace {
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);
}
}
Code Runner Challenge
Predict every line before running, then change < to <= and explain the new output
View IPYNB Source
// CODE_RUNNER: Run it, then try scores[-1] inside the try block and see what happens
public class BoundsCheck {
public static void main(String[] args) {
int[] scores = {85, 92, 78, 90};
System.out.println("Valid indices: 0 to " + (scores.length - 1));
System.out.println("scores[3] = " + scores[3]);
try {
System.out.println(scores[4]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("scores[4] failed: " + e.getMessage());
}
}
}
BoundsCheck.main(null);
Common length mistake:
Replace the sample values with your own, then run it
public class MyWeek {
public static void main(String[] args) {
// Try writing your own code here first!
// Sample answer:
int[] sleepHours = {7, 6, 8, 5, 9};
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
System.out.println("Nights tracked: " + sleepHours.length);
System.out.println("First night (" + days[0] + "): " + sleepHours[0]);
int last = sleepHours.length - 1;
System.out.println("Last night (" + days[last] + "): " + sleepHours[last]);
sleepHours[1] = 7;
System.out.println("After update, " + days[1] + ": " + sleepHours[1]);
}
}
Code Runner Challenge
Replace the sample values with your own, then run it
View IPYNB Source
scores.length // arrays use a field
name.length() // String uses a method
list.size() // ArrayList uses size()
Terminology:
The compiler cannot catch a bad index. The error only happens at runtime, when the program actually reaches for the missing slot.
C. Tracing a loop. The DoubledTrace class walks every index and prints a changed value
Fill in the blanks, then run it
public class ScoreReport {
public static void main(String[] args) {
final String STUDENT_NAME = "Your Name";
// 1. Five quiz scores as an int array (initializer list)
int[] quizzes = {0, 0, 0, 0, 0};
// 2. Print how many quizzes and the last score (use length, not a hardcoded number)
System.out.println(STUDENT_NAME + "'s quiz report");
// 3. Print every score with its index using a for loop (check your condition!)
// 4. Reach for an index that doesn't exist inside try/catch and print the message
}
}
Code Runner Challenge
Fill in the blanks, then run it
View IPYNB Source
// CODE_RUNNER: Predict every line before running, then change < to <= and explain the new output
public class DoubledTrace {
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);
}
}
DoubledTrace.main(null);
Click to reveal the trace
i = 0→85 * 2→ prints170i = 1→92 * 2→ prints184i = 2→78 * 2→ prints156i = 3→90 * 2→ prints180i = 4→4 < 4is false → loop ends
With <=, the loop runs once more with i = 4, prints all four values, then throws ArrayIndexOutOfBoundsException.
Hacks & Practice Tasks
Popcorn Hack
2-minute challenge: build arrays about your week, then run the cell.
- Hours of sleep for the last 5 nights (whole numbers, initializer list)
- The names of those 5 days (text)
- How many nights you tracked (use
length) - The first night and the last night (no hardcoded
4for the last one) - Change one night’s value and print it again
Replace the sample values with your own.
// CODE_RUNNER: Replace the sample values with your own, then run it
public class MyWeek {
public static void main(String[] args) {
// Try writing your own code here first!
// Sample answer:
int[] sleepHours = {7, 6, 8, 5, 9};
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri"};
System.out.println("Nights tracked: " + sleepHours.length);
System.out.println("First night (" + days[0] + "): " + sleepHours[0]);
int last = sleepHours.length - 1;
System.out.println("Last night (" + days[last] + "): " + sleepHours[last]);
sleepHours[1] = 7;
System.out.println("After update, " + days[1] + ": " + sleepHours[1]);
}
}
MyWeek.main(null);
MCQ Check
6 questions, one at a time. Answer, check, then go to the next one. At the end, copy the score line into your notes.
int[] a = {3, 6, 9, 12};What is a.length?
Using the same array, what does a[a.length - 1] return?
Given int[] nums = new int[5];, which expression throws an ArrayIndexOutOfBoundsException?
Which line correctly stores the size of nums in a variable?
Why store test scores in an array instead of score1, score2, score3, score4?
int[] arr = {2, 4, 6};
for (int i = 0; i <= arr.length; i++) {
System.out.println(arr[i]);
}What happens?
Homework Hack
Task: Build a quiz score report. Store the student’s name in a final variable and five quiz scores in an int array. Print how many quizzes there are and the last score using length, print every score with its index using a loop, then reach for an index that doesn’t exist inside a try/catch and print what happened.
Solution Skeleton:
// CODE_RUNNER: Fill in the blanks, then run it
public class ScoreReport {
public static void main(String[] args) {
final String STUDENT_NAME = "Your Name";
// 1. Five quiz scores as an int array (initializer list)
int[] quizzes = {0, 0, 0, 0, 0};
// 2. Print how many quizzes and the last score (use length, not a hardcoded number)
System.out.println(STUDENT_NAME + "'s quiz report");
// 3. Print every score with its index using a for loop (check your condition!)
// 4. Reach for an index that doesn't exist inside try/catch and print the message
}
}
ScoreReport.main(null);
References
College Board. (2025). AP Computer Science A course and exam description [Effective fall 2025].
Hoffman, B., et al. (2025). 4.2 Data sets. In CSAwesome2: AP CSA Java 2026+. Runestone Academy.
Oracle. (n.d.). Arrays. The Java Tutorials.
Qian, Y., & Lehman, J. (2017). Students’ misconceptions and other difficulties in introductory programming: A literature review. ACM Transactions on Computing Education, 18(1), Article 1.