Skip to main content

Unit 4

30–40% of exam

Data Collections

Unit 4 is the biggest unit on the exam. You store many values together in arrays, ArrayLists and 2D arrays, read data from text files, and write the standard algorithms that search, count, total and rearrange that data. You also trace searching and sorting algorithms and recursive methods, and think about privacy and bias in the data you collect. Two of the four free-response questions come from this unit.

Study this unit

Flashcards (40)Practice questions (83)Computer Science A must-know sheet

Free-response questions on this unit

Write your own answer, then score it with the rubric or with AI.

Big ideas

  • An array has a fixed size, while an ArrayList can grow and shrink
  • Traversing a collection with a loop is the base of almost every algorithm here
  • Removing items while traversing an ArrayList needs care to avoid skipping elements
  • A 2D array is an array of rows, usually traversed in row-major order
  • Binary search and merge sort repeatedly cut a problem in half
  • Data can be biased or incomplete, and collecting it puts people's privacy at risk

Full unit reviews

Longer videos that cover the whole unit. Good for a first pass or a final review.

  • AP Computer Science A Unit 4 Review | Data Collections (AP CSA Exam Prep)

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

  • 2026 AP Computer Science A Exam Review - CRASH COURSE for Unit 4, Part 1: Data Collections

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

  • 2026 AP Computer Science A Exam Review - CRASH COURSE for Unit 4, Part 2: Data Collections

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

Collecting and storing personal data puts people's privacy at risk, so programmers should try to protect it. Data can be biased, incomplete or inaccurate, and algorithmic bias is when a program's repeated errors create unfair results for certain groups. A data set gathered for one question may not be the right one for answering a different question.

Key terms

  • privacy
  • personal data
  • algorithmic bias
  • data quality
  • AP Computer Science A - Topic 4.1: Ethical and Social Issues Around Data Collection

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

  • AP CSA Data Collections – Ethics and Social Issues Around Data Collection

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

  • Algorithmic Bias and Fairness: Crash Course AI #18

    CrashCourseWatch on YouTube (opens in a new tab)

  • AP CS A - 7.7 Ethical Issues Around Data Collection

    CodeHSWatch on YouTube (opens in a new tab)

  • Algorithmic bias | Intro to CS - Python | Khan Academy

    Khan AcademyWatch on YouTube (opens in a new tab)

Read the review notes: 4.1 Ethical and Social Issues Around Data Collection

A few quick questions on this topic, with the answers explained.

New in the 2025 course. A data set is a collection of related pieces of information that a program can analyze to answer a question. Programs usually look at the values one at a time and process each one, and sketching the data in a table or chart first helps you plan the algorithm.

Key terms

  • data set
  • processing data one value at a time
  • table
  • 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)

Read the review notes: 4.2 Introduction to Using Data Sets

A few quick questions on this topic, with the answers explained.

An array stores a fixed number of values of the same type, and its size can't change once it's created. You create one with new int[5] (elements start at default values like 0, false or null) or with an initializer list like {3, 1, 4}. Valid indexes run from 0 to arr.length - 1; anything else throws an ArrayIndexOutOfBoundsException.

Key terms

  • array
  • element
  • index
  • length
  • initializer list
  • ArrayIndexOutOfBoundsException
  • AP Computer Science A - Topic 4.3 - Part 1: Array Creation and Access

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

  • AP CSA Unit 4 Array Basics (2025-2026)

    PickcodeWatch on YouTube (opens in a new tab)

  • AP CSA Data Collections – One Dimensional Arrays

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

  • AP Computer Science A - Topic 4.3 - Part 2: Array Creation and Access

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

  • Learn Java arrays in 9 minutes! 🍎

    Bro CodeWatch on YouTube (opens in a new tab)

  • Arrays (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

Read the review notes: 4.3 Array Creation and Access

A few quick questions on this topic, with the answers explained.

Traversing an array means using a loop to visit its elements, either by index with a for or while loop or with an enhanced for loop (for (int x : arr)). The enhanced for variable is a copy of each element, so assigning to it doesn't change the array, though calling methods on it can change the objects the array refers to.

Key terms

  • traversal
  • enhanced for loop
  • indexed loop
  • loop variable copy
  • AP Computer Science A - Topic 4.4 - Part 1: Array Traversals

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

  • AP CSA – Unit 6: Array – Lesson 4: The Enhanced For Loop

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

  • Traversing an Array (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.4 - Part 2: Array Traversals

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

  • java for-each loop 🔃

    Bro CodeWatch on YouTube (opens in a new tab)

Read the review notes: 4.4 Array Traversals

A few quick questions on this topic, with the answers explained.

Standard array algorithms include finding a minimum or maximum, computing a sum or average, checking whether at least one or all elements have a property, counting matches, comparing consecutive pairs, detecting duplicates, shifting or rotating elements, and reversing the array. Most are a single traversal with a well-chosen variable to track the answer.

Key terms

  • min and max
  • sum and average
  • count
  • consecutive pairs
  • duplicates
  • shift, rotate and reverse
  • AP Computer Science A - Topic 4.5 - Part 1: Implementing Array Algorithms

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

  • AP CSA Unit 4 Array Algorithms (2025-2026)

    PickcodeWatch on YouTube (opens in a new tab)

  • Find The Maximum Number In A Java Array

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.5 - Part 2: Implementing Array Algorithms

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

  • AP Computer Science A - Topic 4.5 - Part 3: Implementing Array Algorithms

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

  • Reversing the Values in an Array (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

Read the review notes: 4.5 Implementing Array Algorithms

A few quick questions on this topic, with the answers explained.

New in the 2025 course. A text file keeps data after the program stops running. You open one with new Scanner(new File("data.txt")), add throws IOException to the method header, and import File and IOException from java.io and Scanner from java.util. You read with methods like nextInt, next and nextLine, loop with while (input.hasNext()), call close() when you're done, and use split to break a line into a String array.

Key terms

  • File
  • Scanner
  • throws IOException
  • hasNext
  • nextLine
  • split
Read the review notes: 4.6 Using Text Files

A few quick questions on this topic, with the answers explained.

Integer and Double are wrapper classes that turn primitive values into immutable objects, which matters because an ArrayList can only hold objects. Java converts automatically: autoboxing turns an int into an Integer, and unboxing turns it back. Integer.parseInt and Double.parseDouble turn a String into a number.

Key terms

  • wrapper class
  • Integer
  • Double
  • autoboxing
  • unboxing
  • Integer.parseInt
Read the review notes: 4.7 Wrapper Classes

A few quick questions on this topic, with the answers explained.

An ArrayList<E> is a resizable list of object references from java.util. Its key methods are size(), add(obj), add(index, obj), get(index), set(index, obj) and remove(index); adding or removing in the middle shifts the later elements and changes their indexes.

Key terms

  • ArrayList
  • generic type <E>
  • size
  • add
  • get and set
  • remove
  • AP Computer Science A - Topic 4.8 - Part 1: ArrayList Methods

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

  • AP CSA – Unit 7: ArrayList – Lesson 2: ArrayList Methods

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

  • ArrayList in Java Tutorial #36

    Alex LeeWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.8 - Part 2: ArrayList Methods

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

  • AP Computer Science A - Topic 4.8 - Part 3: ArrayList Methods

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

  • Learn Java arraylists in 9 minutes! 📃

    Bro CodeWatch on YouTube (opens in a new tab)

Read the review notes: 4.8 ArrayList Methods

A few quick questions on this topic, with the answers explained.

You traverse an ArrayList with an indexed loop using get(i) and size(), or with an enhanced for loop. Removing elements while looping forward can skip the next element, so adjust the index or loop backward, and never add or remove inside an enhanced for loop, which can throw a ConcurrentModificationException.

Key terms

  • ArrayList traversal
  • skipped elements
  • IndexOutOfBoundsException
  • ConcurrentModificationException
  • AP Computer Science A - Topic 4.9 - Part 1: ArrayList Traversals

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

  • AP CSA Data Collections – ArrayList Traversals

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

  • Traversing a Java ArrayList (Printing an ArrayList)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.9 - Part 2: ArrayList Traversals

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

  • AP Computer Science A - Topic 4.9 - Part 3: ArrayList Traversals

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

Read the review notes: 4.9 ArrayList Traversals

A few quick questions on this topic, with the answers explained.

The array algorithms all work on an ArrayList too, and the list's methods make inserting and deleting elements much easier. Some problems need you to traverse two collections at once, such as comparing two lists or building one list from another.

Key terms

  • insert and delete
  • parallel traversal
  • filtering a list
  • standard algorithms
  • AP Computer Science A - Topic 4.10 - Part 1: Implementing ArrayList Algorithms

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

  • AP CSA Data Collections – Implementing ArrayList Algorithms

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

  • Search And Remove From ArrayLists (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.10 - Part 2: Implementing ArrayList Algorithms

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

  • ArrayList Algorithms in Java | AP CSA Unit 7

    Stefan WebsterWatch on YouTube (opens in a new tab)

  • 2026 AP Computer Science A Exam Review - Exploring FRQ 3: Data Analysis with ArrayLists

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

Read the review notes: 4.10 Implementing ArrayList Algorithms

A few quick questions on this topic, with the answers explained.

A 2D array is an array of arrays, written like int[][] grid = new int[3][4] for 3 rows and 4 columns. You access an element with grid[row][col]; grid.length is the number of rows and grid[0].length is the number of columns. On the exam every 2D array is rectangular.

Key terms

  • 2D array
  • row
  • column
  • arr[row][col]
  • row and column length
Read the review notes: 4.11 2D Array Creation and Access

A few quick questions on this topic, with the answers explained.

You traverse a 2D array with nested loops. Row-major order goes across each row before moving down, and column-major order goes down each column before moving across. In a nested enhanced for loop, the outer variable is a whole row (a 1D array) and the inner variable is one element.

Key terms

  • nested loops
  • row-major order
  • column-major order
  • enhanced for over rows
  • AP Computer Science A - Topic 4.12: 2D Array Traversals

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

  • AP CS A - 8.2 Traversing 2D Arrays

    CodeHSWatch on YouTube (opens in a new tab)

  • Traversing a 2 Dimensional Array (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • Five 2D array patterns you should know BLINDFOLDED (AP CSA/Java)

    CodeSlateWatch on YouTube (opens in a new tab)

Read the review notes: 4.12 2D Array Traversals

A few quick questions on this topic, with the answers explained.

The standard array algorithms carry over to 2D arrays, either across the whole grid or for one row, one column or another section. Typical tasks are totals and averages for each row or column, finding the largest value, counting matches, checking neighbors, and shifting or reversing a row or column.

Key terms

  • row and column totals
  • subsection
  • neighbors
  • 2D min and max
  • AP Computer Science A - Topic 4.13 - Part 1: 2D Array Algorithms

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

  • AP CSA Data Collections – Implementing 2D Array Algorithms

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

  • 2D Array Algorithms in Java | AP CSA Unit 8

    Stefan WebsterWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 4.13 - Part 2: 2D Array Algorithms

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

  • AP Computer Science A - Topic 4.13 - Part 3: 2D Array Algorithms

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

  • 2026 AP Computer Science A Exam Review - Exploring FRQ 4: 2D Arrays

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

Read the review notes: 4.13 Implementing 2D Array Algorithms

A few quick questions on this topic, with the answers explained.

Linear search checks the elements one at a time, from either end, until it finds the target or runs out of elements. It works on unsorted data in arrays and ArrayLists, and in a 2D array you run it on each row in turn.

Key terms

  • linear (sequential) search
  • target
  • return -1 when not found
Read the review notes: 4.14 Searching Algorithms

A few quick questions on this topic, with the answers explained.

Selection sort repeatedly finds the smallest (or largest) remaining element and swaps it into its final position. Insertion sort takes the next element and shifts larger elements in the sorted part over to slide it into place, so its position may still change later. You need to be able to trace each pass of both.

Key terms

  • selection sort
  • insertion sort
  • swap
  • shift
  • pass
Read the review notes: 4.15 Sorting Algorithms

A few quick questions on this topic, with the answers explained.

A recursive method calls itself. It needs at least one base case that stops the recursion and a recursive call that moves toward it, and each call gets its own copies of the parameters and local variables. Anything done with recursion can also be done with a loop. On the exam you trace recursive methods to find what they return or print; you won't be asked to write one.

Key terms

  • recursion
  • base case
  • recursive call
  • call stack
  • tracing
Read the review notes: 4.16 Recursion

A few quick questions on this topic, with the answers explained.

Recursion can walk through a string, array or ArrayList one piece at a time. Binary search only works on sorted data: it checks the middle element and throws away the half that can't hold the target, so it's usually much faster than linear search. Merge sort recursively splits a list into halves, sorts each half and merges them back together. You need to trace each step of both.

Key terms

  • binary search
  • sorted data
  • merge sort
  • divide and conquer
  • merge
Read the review notes: 4.17 Recursive Searching and Sorting

A few quick questions on this topic, with the answers explained.