SAT / PSAT
SAT / PSAT Prep
History & Social Science
AP World History AP US History AP European History AP Human Geography AP US Government & Politics AP Psychology AP Macroeconomics AP Microeconomics
English
AP English Language & Composition AP English Literature & Composition
Math & Computer Science
AP Calculus AB/BC AP Precalculus AP Statistics AP Computer Science A AP Computer Science Principles
Sciences
AP Biology AP Chemistry AP Environmental Science AP Physics 1 AP Physics 2
World Languages & Arts
AP Spanish Language AP Art History AP Music Theory Start studying →
Unit 4 · Data Collections Flashcards Cheat Sheet Essentials Visual Review MC Practice FRQ Practice

AP Computer Science A Unit 4 Visual Review

A topic-by-topic visual walkthrough of Data Collections — arrays, ArrayLists, 2D arrays, searching and sorting algorithms, and recursion.

← Back to Unit 4 hub
TOPIC 4.1 Ethical & Social Issues Around Data Collecting and using data carries responsibility Programs that gather data must respect privacy, security, and consent. How data is collected and used has real consequences for the people it describes. Key concerns • PRIVACY: personal data can be misused. • SECURITY: data must be protected. • BIAS: skewed data → unfair results. • CONSENT: users should know & agree. Responsible practice Collect only what you need, store it securely, and be transparent about use. Anonymize where possible and let users control their own information. Legal ≠ ethical — consider impact. Handle data with privacy, security, and consent in mind — guard against bias. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.2 Introduction to Using Data Sets Data structures store many values under one name Instead of hundreds of separate variables, a COLLECTION groups related values so you can process them with a loop. This unit covers arrays, ArrayLists, and 2D arrays. Array Fixed size, set at creation. Holds primitives or objects. ArrayList Grows & shrinks dynamically. Holds objects only. 2D Array A grid of rows and columns. Good for tables, images, boards. Collections group values so a loop can process them: array, ArrayList, 2D array. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.3 Array Creation and Access int[] a = {8, 3, 5, 9, 2}; 80 31 52 93 24 indices run 0 … length−1 a[0] → 8 a[3] → 9 Creating arrays int[] a = new int[5]; 5 slots, all default to 0 a.length // 5 (a field, no ()) Length is fixed once created. Out-of-bounds errors a[5] or a[-1] on a length-5 array throws ArrayIndexOutOfBounds- Exception. Valid indices: 0 to a.length − 1. Arrays are fixed-size and zero-indexed; use a.length (a field, no parentheses). The Review Hub · AP Computer Science A Unit 4 TOPIC 4.4 Array Traversals // indexed for loop — gives you i for (int i = 0; i < a.length; i++) sum += a[i]; // for-each — reads each value for (int v : a) sum += v; Which loop to use INDEXED for: when you need the index i, or want to modify elements. FOR-EACH: when you only READ each value in order — cleaner, no index bugs. for-each can't assign back into the array. Traversal = visit every element once The foundation of nearly every array algorithm: summing, counting, searching, finding a min or max. Use an indexed for when you need i; a for-each to simply read each value. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.5 Implementing Array Algorithms // find the maximum value int max = a[0]; for (int i = 1; i < a.length; i++) if (a[i] > max) max = a[i]; // max now holds the largest Standard array algorithms • SUM / AVERAGE of all elements • MIN / MAX value or its index • COUNT elements meeting a test • does an element exist? (search) • shift or reverse elements Start max at a[0], not 0 Initializing max = 0 fails if all values are negative. Seed it with the FIRST element instead. Traverse to compute sum, min/max, and counts — seed min/max with a[0]. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.6 Using Text Files // read a file line by line Scanner f = new Scanner( new File("data.txt")); while (f.hasNextLine()) { String line = f.nextLine(); } The hasNext / next pattern hasNextLine() checks if more input remains BEFORE you read. nextLine() reads and consumes the next line of text. Same Scanner idea as keyboard input. A Scanner reads files the same way it reads the keyboard Loop with hasNextLine()/hasNextInt() and read until the file runs out — no need to know its size in advance. Read files with a Scanner: while (hasNextLine()) { nextLine(); }. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.7 Wrapper Classes Wrappers let primitives act like objects Integer wraps int; Double wraps double. ArrayList stores OBJECTS only, so primitives must be wrapped. ArrayList<Integer> list = new ArrayList<Integer>(); // autoboxing & unboxing Integer obj = 5; // box int x = obj; // unbox Java converts automatically between int and Integer. Handy wrapper constants Integer.MAX_VALUE Integer.MIN_VALUE Integer.parseInt("42") Compare Integers with .equals, not ==. Integer / Double wrap primitives so they fit in an ArrayList — autoboxing converts for you. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.8 ArrayList Methods list.add(x) append x to the end list.add(i, x) insert x at index i, shift right list.get(i) the element at index i list.set(i, x) replace index i, return old value list.remove(i) delete index i, shift left list.size() number of elements (a method!) size() for lists, length for arrays ArrayList uses list.size(); arrays use a.length; Strings use s.length(). Mixing them up won't compile. Know add, get, set, remove, size — add/remove shift the other elements. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.9 ArrayList Traversals Removing while looping — a trap Calling remove(i) shifts every later element left by one. If you then do i++, you SKIP the shifted element. Fix: don't increment i after a remove, or loop BACKWARD. // safely remove all zeros int i = 0; while (i < list.size()) { if (list.get(i) == 0) list.remove(i); else i++; } Don't modify a list during a for-each Adding or removing inside a for-each loop throws a ConcurrentModificationException — use an index loop. When removing, don't advance i after a remove — and never remove during a for-each. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.10 Implementing ArrayList Algorithms Same patterns as arrays Sum, count, min/max, and search all work the same — just use get(i) and size() instead of a[i] and length. Plus you can insert & delete dynamically. // count values above 100 int c = 0; for (int v : list) if (v > 100) c++; // c holds the tally Building a new list from an old one A common pattern: traverse the source and add() the elements you want into a fresh ArrayList. This avoids the shifting bugs of removing in place, and keeps the original list unchanged. ArrayList algorithms mirror arrays with get(i) and size() — plus dynamic insert/delete. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.11 2D Array Creation and Access int[][] g = new int[3][4]; c0c1c2c3 r0r1r2 g[1][1] 3 rows × 4 columns = 12 cells Row first, then column g[row][col] g.length = number of ROWS (3) g[0].length = number of COLUMNS (4) Initializer syntax int[][] g = {{1,2},{3,4}}; Each inner {} is one row. AP 2D arrays are always rectangular. A 2D array is g[row][col]; g.length is rows, g[0].length is columns. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.12 2D Array Traversals // nested loops visit every cell (row-major order) for (int r = 0; r < g.length; r++) for (int c = 0; c < g[r].length; c++) sum += g[r][c]; Outer = rows, inner = columns The outer loop picks a row; the inner loop walks across that row's columns. This is "row-major" order. for-each version for (int[] row : g) for (int v : row) ... Each outer item is a whole row (int[]). Traverse a 2D array with nested loops: outer rows, inner columns. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.13 Implementing 2D Array Algorithms // sum of one column c int colSum = 0; for (int r = 0; r < g.length; r++) colSum += g[r][c]; // hold c fixed, vary the row Common 2D tasks • Sum a whole grid, a row, or a column • Find the max in the grid • Count cells meeting a condition • Read/update neighbors (game boards) Row sum: fix r, vary c. Column sum: fix c, vary r. Watch which index you hold fixed Row work fixes the row and loops columns; column work fixes the column and loops rows. Swapping them is a classic bug. For rows fix r & loop c; for columns fix c & loop r. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.14 Searching Algorithms Linear search Check each element in order until you find the target or reach the end. Works on ANY list (sorted or not). Up to n checks for n elements. "linear" cost — grows with n. Binary search Requires a SORTED list. Check the middle, then discard half each step. Far faster: about log₂(n) checks (1000 items → ~10 checks). Only valid when the data is sorted. BINARY SEARCH halves the range each step: 16 items 8 4 2 1 Linear search works on any list; binary search needs sorted data but is much faster. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.15 Sorting Algorithms Selection sort Repeatedly find the SMALLEST remaining value and swap it to the front. Grows the sorted region one item per pass from the left. Uses nested loops → about n² comparisons. Insertion sort Take the next element and INSERT it into its correct spot among those already sorted. Like sorting a hand of cards. Fast on nearly-sorted data. Also about n² in the worst case. Both build a growing "sorted region" Selection sort SELECTS the min for the next slot; insertion sort INSERTS each item where it belongs. Trace them carefully. Selection picks the min each pass; insertion inserts into the sorted part — both ~n². The Review Hub · AP Computer Science A Unit 4 TOPIC 4.16 Recursion // factorial: n! = n * (n-1)! int fact(int n) { if (n <= 1) return 1; // base case return n * fact(n - 1); // recursive case } A method that calls itself Every recursion needs two things: • BASE CASE: stops the recursion. • RECURSIVE CASE: calls itself on a SMALLER input, moving toward the base. No base case → infinite recursion (StackOverflow). Trace by unwinding the calls fact(3) = 3·fact(2) = 3·2·fact(1) = 3·2·1 = 6. Each call waits for the smaller one to return. Recursion needs a base case and a smaller recursive call toward it. The Review Hub · AP Computer Science A Unit 4 TOPIC 4.17 Recursive Searching & Sorting Recursive binary search Look at the middle of the range. • equal → found it (base case). • target smaller → recurse on LEFT half. • target larger → recurse on RIGHT half. Empty range → not found (base case). Merge sort (recursive) DIVIDE the array in half, recursively sort each half, then MERGE them back in sorted order. A "divide and conquer" algorithm — much faster than n² on large data. Divide and conquer Both split the problem into smaller pieces and solve each recursively — the recursive form of binary search and sorting. Recursive binary search and merge sort use divide-and-conquer to beat linear work. The Review Hub · AP Computer Science A Unit 4
1 / 17

How to use the visual review

Spend 30 seconds per slide before clicking next. Look at the code, then ask yourself: "Could I trace this traversal or algorithm from memory?"

Use the fullscreen button () on desktop for the best experience. Use arrow keys to navigate. Tap "Show all slides" to jump around.

This is great for review the night before the exam — fast, visual, and covers every idea you need to recognize in Unit 4.