4.02 Introduction to Using Data Sets
Work with data sets by identifying variables, interpreting records, and using data to answer meaningful questions.
1. 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)
2. LxD Cycle Process
Empathize: Students treat index 1 as the first element, write <= against .length in loop conditions, and type scores.length() because that is how String works. Reviews of novice programming research list loop boundaries and array indexing among the recurring difficulties for beginners (Qian & Lehman, 2017).
Define:
- POV: CSA students need to see an index as “how many slots to skip from the start,” because that one idea explains why counting starts at 0, why the last index is
length - 1, and why<=breaks a loop. - Learning Goal: Students will declare and initialize arrays, access and modify elements by index, use
lengthto find the last element, and predict when code throws anArrayIndexOutOfBoundsException.
Ideate:
- HMW Question: How might we get students to check the last valid index before they write a loop condition?
- HMW Question: How might we show that an array replaces a pile of separate variables and scales from 4 values to 400?
- Activity: Build an array about your own week, answer index and bounds questions in the MCQ, then build a quiz score report.
Prototype:
- A reference guide, runnable Java examples, a personalized array popcorn hack, an MCQ knowledge check, and a scaffolded grade rubric.
- Students revise loop conditions and index expressions after seeing runtime errors or feedback.
- Excellence means explaining why an index is valid or invalid, using
lengthinstead of hardcoded numbers, and improving the first attempt rather than only producing working code.
Test:
- Ask peers in peer review to complete the practice without additional explanation.
- Observe whether peers use
arr.length - 1for the last element and<in loop conditions. - Compare your lesson with another that is posted.
- Use the findings to revise any instruction or rubric criterion that did not guide students clearly.
- On submission, collect evidence from runner output, MCQ results, AI grading and student explanations.
- After teaching, grading and analysis, come back and revise lesson to complete teaching cycle for continuous improvement.
3. 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.
4. 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 (5 minutes)
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.
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);
5. 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:
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
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
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:
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
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.
6. Hacks & Practice Tasks
Prepare your submission IPYNB
- Create a new notebook in your portfolio homework area:
_notebooks/homework. - Add one raw cell at the top with the frontmatter (same fields as your other homework notebooks).
- Add code cells for the Popcorn Hack and the Homework Hack. Make sure every cell runs with visible output.
- Submit the link to your published page at the bottom of this page, and paste this in the description box:
Lesson: CSA 4.2 Introduction to Using Data Sets
MCQ 4.2: <paste your result line, such as 5/6 | answers: B,B,C,C,B,B>
Popcorn: int array and String array declared, length/first/last printed, one value changed (yes/no)
Homework: final student name used (yes/no)
Homework: five int scores = <s1>, <s2>, <s3>, <s4>, <s5>
Homework: length printed = <value>, last score using length - 1 = <value>
Homework: loop prints every index using < arr.length (yes/no)
Homework: out-of-bounds caught, message printed = <message>
Submission Safety Rules (Read First)
- One class per cell, ending with
ClassName.main(null);. - Run each cell and leave the output showing.
- Use your own values, not the sample answer.
- Include your MCQ score.
- Use
##headings or smaller.
Popcorn Hack (In-Class)
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 submission notes.
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);
Grading Plan (1 Point Total)
| Part | Points | What earns the points |
|---|---|---|
| Popcorn | 0.2 | An int array and a String array, length/first/last printed using length, one value changed, and the cell runs. |
| MCQ | 0.2 | 5 or 6 correct. 0.15 for 3 or 4, 0.1 if every question was answered. |
| Homework: array | 0.15 | final student name and five quiz scores in an int array. |
| Homework: length | 0.15 | Number of quizzes and last score printed using length, no hardcoded index. |
| Homework: loop | 0.15 | Every score printed with its index using i < quizzes.length. |
| Homework: bounds | 0.15 | Out-of-bounds access caught and a message printed. |
| Total | 1.0 |
Quick Validation Checklist
- Each cell ends with
ClassName.main(null);and shows output. - MCQ score in the notes.
finalon the student name.- Last element reached with
length - 1, not a hardcoded number. - Loop condition uses
<, not<=. - Out-of-bounds error caught and printed.
7. Lesson Revisions
Revision Made: Lesson authors: what you changed because of it.
8. Feedback Evidence
Feedback Received: Lesson authors: what your peers said in the practice run.
9. 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.