Skip to main content

Unit 4 · Topic 4.2

4.2 Introduction to Using Data Sets

New in the 2025 course. A data set is a collection of related information that a program can analyze to answer a question. This topic is about thinking through a data set before you code: what the values are, how you'll look at them one at a time, and how a table or chart helps you plan.

Key terms

  • data set
  • processing data one value at a time
  • table

What a data set is

A data set is a group of related values or records gathered in one place. It might be a week of temperatures, the scores from every game in a season, a list of songs with their lengths and play counts, or the names of everyone in a club.

Data sets are worth collecting because a program can dig through them to answer questions: Which day was hottest? How many games did we win? Which song is played most?

One value at a time

A program works through a data set one value at a time, doing whatever the goal calls for with each one. To find the hottest day, you look at each temperature and keep track of the biggest so far. To count wins, you look at each game and add 1 when it was a win.

In Java, a data set with one value per item, like the temperatures, fits in an array or ArrayList of numbers. When each item has several attributes, like a song's title, length and play count, each item is usually an object, and the data set is a list of those objects.

That idea, visit each value and update something, is the core of almost every algorithm in this unit. The rest of Unit 4 gives you Java tools for it: arrays (4.3), ArrayLists (4.8), 2D arrays (4.11) and text files (4.6).

Plan with a table or chart

Before you write code, sketch the data. A table with one row per value, or per item with several columns, makes the pattern clear. A chart can show at a glance which values stand out. Then write the steps in plain words, and only then write Java.

Here's one week of rainfall, in inches, as a table:

DayMonTueWedThuFriSatSun
Rain0.00.41.20.00.30.00.8

Questions to ask about any data set

  • What does each value represent, and what are its units?
  • What type should it be in Java: int, double, String, boolean, or an object with several attributes?
  • Does the question need every value, or only some (like the values above a cutoff)?
  • What should happen with unusual values, like missing data (4.1) or a set with no values at all?

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    From a question to an algorithm

    Using the rainfall table above, a student wants to know how many days had rain and how much fell in total. Plan the algorithm in words, then check the Java version. What does it print?double[] rain = {0.0, 0.4, 1.2, 0.0, 0.3, 0.0, 0.8}; int wetDays = 0; double total = 0.0; for (double r : rain) { total += r; if (r > 0.0) { wetDays++; } } System.out.println(wetDays + " wet days"); System.out.println("total: " + total);

    Show the solution
    1. Step 1: Plan in words: start a counter of wet days at 0 and a running total at 0. Look at each day's rainfall. Add it to the total. If it's more than 0, add 1 to the counter.
    2. Step 2: The Java matches the plan line for line. The loop visits each value once, in order.
    3. Step 3: Wet days are Tue, Wed, Fri and Sun: 4 days.
    4. Step 4: The total is 0.4 + 1.2 + 0.3 + 0.8 = 2.7 inches. (The dry days add 0.)

    Answer: It prints 4 wet days and then total: 2.7.

  2. Example 2

    Planning with several attributes

    A music app has a data set of songs. For each song it stores a title, a length in seconds and a number of plays. Plan an algorithm to find the title of the most-played song. Don't write code yet.

    Show the solution
    1. Step 1: Sketch a table with three columns: title, length, plays. Each row is one song.
    2. Step 2: The question needs only two columns: plays (to compare) and title (to report). Length doesn't matter here.
    3. Step 3: Plan: remember the first song's plays and title as the best so far. Look at each remaining song. If its plays are greater than the best so far, replace both the best plays and the best title.
    4. Step 4: Decide on the edge case: if two songs tie, this plan keeps the earlier one, because it only replaces on "greater than".

    Answer: Track the best play count and its title; visit each song once and replace both when a song has more plays. After the last song, the saved title is the answer.

Common mistakes

  • Jumping straight into code without knowing what each value means or what the question needs.
  • Forgetting edge cases, like ties or a data set with no values.
  • Tracking only the winning number when the question asks for something about it, like the song's title. Save both.

On the exam

  • This topic was added in the 2025 course update. Expect questions that describe a data set in a table and ask which algorithm, or which code segment, answers a question about it.
  • Free-response Questions 3 and 4 hand you data in an ArrayList or a 2D array. Sketching a tiny example before coding is the fastest way to get the loop right.

Connected topics

Videos

  • AP Computer Science A - Topic 4.2: Introduction to Using Data Sets

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • AP CSA Data Collections – Data Sets

    Goldie's Math EmporiumWatch on YouTube (opens in a new tab)

  • What is a Dataset?

    MMU Q-StepWatch on YouTube (opens in a new tab)

  • Data Analysis 1: What is Data? - Computerphile

    ComputerphileWatch on YouTube (opens in a new tab)

Check yourself

5 questions on 4.2 Introduction to Using Data Sets. Pick an answer to see if you got it, and why.

Question 1 of 5

A store has a data set of its total sales for each month of the year. The manager wants to know which month had the biggest increase over the month before it. After sketching the data in a table, which plan should the program follow?

DaySteps
Monday6,200
Tuesday8,400
Wednesday10,150
Thursday7,900
Friday8,000
Saturday12,500
Sunday4,300

Invented data

Question 2 of 5

Which of the following questions can be answered using only this data set?

Question 3 of 5

The data is stored in the array steps, declared as follows.int[] steps = {6200, 8400, 10150, 7900, 8000, 12500, 4300};A program processes the values one at a time to count the days with at least 8,000 steps. What count should it produce?

Question 4 of 5

A program will find the longest run of back-to-back days with at least 8,000 steps, looking at the values one at a time in order. Which of the following best describes what it needs to keep track of as it goes?

Question 5 of 5

What is printed as a result of executing the following code segment?int[] steps = {6200, 8400, 10150, 7900, 8000, 12500, 4300}; int current = 0; int longest = 0; for (int s : steps) { if (s >= 8000) { current++; if (current > longest) { longest = current; } } else { current = 0; } } System.out.println(longest);

0 of 5 answered