The Stacks
UNIT 4: DATA COLLECTIONS · CHEAT SHEET

Data Collections — the one-page version

Every key term and every exam tip from the 17 topics in this unit. Print it, fold it, read it on the bus.

4.1Ethical and Social Issues Around Data Collection

Personal data
information that identifies or could identify an individual
Informed consent
users agreeing to data collection after understanding what and why
Tip: Same rule as CSP's Big Idea 5: the correct answer minimizes collection, requires consent, considers who's harmed, and acknowledges bias. Absolute or one-sided options are distractors.

4.2Introduction to Using Data Sets

Data set
a collection of related records, each with fields
Record
one item in a data set, such as one row
Data cleaning
fixing or removing invalid, duplicate, or missing data before analysis
Tip: Questions here are conceptual: which structure fits which data, and what step (cleaning, filtering, aggregating) a description refers to. The code for all of it comes in the following topics.

4.3Array Creation and Access

Array
a fixed-size, ordered collection of elements of one type
Index
an element's position, from 0 to length - 1
length
the field giving the number of elements in an array
ArrayIndexOutOfBoundsException
the run-time error from using an invalid index
Tip: Every array question: what's the first index (0), the last index (length − 1), and what's the default value? An index equal to length is the exam's favorite exception. And length without parentheses — with parentheses it won't compile.

4.4Array Traversals

Traversal
visiting every element of an array
Enhanced for loop
for (type x : arr) — iterates over elements without an index
Tip: If the question modifies the array through the loop variable of an enhanced for loop, the array is unchanged — that's the trick. If it calls a mutator method on an object element, the object is changed. Index needed? Standard loop.

4.5Implementing Array Algorithms

In-place
modifying the array itself rather than building a new one
Mode
the value that appears most often
Tip: Any algorithm touching arr[i + 1] must stop at length - 1; touching arr[i - 1] must start at 1. For "any" vs "all," the return inside the loop is the opposite of the return after it.

4.6Using Text Files

File
a class representing a file on disk, passed to a Scanner to read it
FileNotFoundException
a checked exception thrown when a file can't be opened; must be declared
hasNextLine()
returns true if another line remains to be read
Tip: Three things the exam checks: the throws FileNotFoundException clause, the hasNext…/next… pair matching (hasNextLine with nextLine, hasNextInt with nextInt), and closing the Scanner. If a method opens a file and has no throws clause, it won't compile.

4.7Wrapper Classes

Wrapper class
an object type (Integer, Double) that holds a primitive value
Autoboxing
automatic conversion from a primitive to its wrapper
Unboxing
automatic conversion from a wrapper to its primitive
Integer.parseInt
converts a String to an int
Tip: You can't write ArrayList<int> — that's a guaranteed compile-error question. Everywhere else, treat Integer like int and let autoboxing work, except: never compare Integers with ==, and remember a null Integer crashes when unboxed.

4.8ArrayList Methods

ArrayList
a resizable list of objects
size()
the number of elements in an ArrayList
add(index, obj)
inserts, shifting later elements right
remove(index)
removes and returns, shifting later elements left
set(index, obj)
replaces and returns the old element; size unchanged
Tip: Rewrite the list in brackets after every call. set doesn't change size; add(i, x) and remove(i) do, and they shift everything after i. remove and set both return the element that was there — questions sometimes use that return value.

4.9ArrayList Traversals

ConcurrentModificationException
the error from modifying an ArrayList inside an enhanced for loop
Backward traversal
looping from size - 1 down to 0, safe for removals
Tip: If a question removes elements in a forward loop, trace it literally — the answer usually has "leftover" elements that were skipped. If asked for the correct version, pick the backward loop or the one with i-- after remove.

4.10Implementing ArrayList Algorithms

Filter
building a new list from elements that meet a condition
Insert in order
adding an element at the position that keeps the list sorted
Tip: FRQ 3 rubrics typically award: correct loop over the list, correct accessor call on each element, correct condition, correct add/remove/return. Write the enhanced for loop first, then the if, then what happens inside. Don't forget to return the new list.

4.112D Array Creation and Access

2D array
an array whose elements are arrays; rows of columns
Row-major
the convention that the first index is the row
Rectangular array
a 2D array where every row has the same length
Tip: Read m[a][b] as "row a, column b" every time — never reverse it. length counts rows; [0].length counts columns. A question that flips these is the standard distractor.

4.122D Array Traversals

Row-major traversal
outer loop over rows, inner over columns
Column-major traversal
outer loop over columns, inner over rows
Tip: Look at which index the outer loop controls. Outer over r → row-major. Outer over c → column-major. When asked for the printed output, write out the grid and read it in that order.

4.13Implementing 2D Array Algorithms

Helper method
a method that handles one piece (like a single row) and is called from a larger algorithm
Tip: FRQ 4 part (a) is usually a 1D-style helper on one row or column; part (b) uses it across the grid. Write the nested loops with r and c named clearly and keep row bound = length, column bound = [0].length. If a return inside nested loops is needed, remember it exits everything.

4.14Searching Algorithms

Linear search
checking elements one by one until the target is found
Binary search
repeatedly halving a sorted range to locate a target
Tip: For "how many times is the target compared," trace mid: write low, high, mid, and the element at mid on each pass. For "which search should be used," unsorted → linear; sorted and large → binary. And binary search's mid uses integer division — truncation matters.

4.15Sorting Algorithms

Selection sort
repeatedly select the smallest remaining element and swap it into place
Insertion sort
insert each element into its correct position within the sorted prefix
Pass
one full iteration of the outer loop of a sort
Tip: Given a mid-sort snapshot: if the first few elements are the globally smallest values in order, it's selection sort. If the first few are in order but a smaller value still sits later in the array, it's insertion sort.

4.16Recursion

Recursion
a method calling itself
Base case
the condition under which a recursive method returns without recursing
Recursive call
the call to the same method with an argument closer to the base case
Tip: Write the calls in a column, arguments decreasing, until the base case returns a concrete value. Then fill in upward. For methods that print, note whether the print happens before or after the recursive call — that reverses the order of output.

4.17Recursive Searching and Sorting

Merge sort
recursively sort halves, then merge them
Merge
combining two sorted sequences into one sorted sequence
Divide and conquer
split a problem into smaller instances, solve recursively, combine
Tip: For merge sort traces, the snapshot after the recursive sorts but before the final merge shows two independently sorted halves — that's the give-away. For recursive binary search, count the calls the same way as 4.14 counts comparisons.