AP® Computer Science A Unit 4 flashcardsData Collections
40 cards · about 10 minutes for the whole deck
Flashcard drill
Space flips the card, arrow keys move, and keys 1 to 4 rate how well you knew it.
- Position
- 1 / 40
- Due
- 40
- Mastered
- 0 / 40
This card: New
Flip the card before you rate it.
Personal privacy
Collecting and storing personal data puts it at risk. Programmers should collect only what's needed and protect it.
Topic 4.1: Ethical and Social Issues Around Data Collection
Algorithmic bias
Repeated, systematic errors in a program that create unfair outcomes for a specific group, often because of biased data.
Topic 4.1: Ethical and Social Issues Around Data Collection
Data quality
Incomplete or inaccurate data, or data collected for a different question, can make a program give wrong answers.
Topic 4.1: Ethical and Social Issues Around Data Collection
Data set
A collection of related pieces of data, like a week of step counts. Programs analyze it to answer a question.
Topic 4.2: Introduction to Using Data Sets
Processing one value at a time
Data set algorithms visit each value in turn and update what they're tracking. Sketching the data in a table helps you plan this.
Topic 4.2: Introduction to Using Data Sets
Array
A fixed-size list of values of one type.
new int[5]makes 5 elements starting at 0; the size can't change later.Topic 4.3: Array Creation and Access
Array
lengtharr.length(no parentheses) is the number of elements. Valid indexes run from 0 toarr.length - 1.Topic 4.3: Array Creation and Access
Initializer list
Creates and fills an array at once:
int[] a = {3, 1, 4};makes an array of length 3.Topic 4.3: Array Creation and Access
ArrayIndexOutOfBoundsExceptionThrown when you use an array index below 0 or at least
length, likearr[arr.length].Topic 4.3: Array Creation and Access
Enhanced
forloopfor (int x : arr)visits each element without an index.xis a copy, so assigning to it doesn't change the array.Topic 4.4: Array Traversals
Consecutive pairs
Compare
arr[i]witharr[i + 1]forifrom 0 tolength - 2, or witharr[i - 1]starting at 1, so you never go out of bounds.Topic 4.5: Implementing Array Algorithms
Shift and rotate
To rotate left, save
arr[0], copy each element one place left, then put the saved value at the end. Shift right by looping from the back.Topic 4.5: Implementing Array Algorithms
Reversing an array
Swap
arr[i]witharr[arr.length - 1 - i]foriup toarr.length / 2. Going all the way swaps everything back.Topic 4.5: Implementing Array Algorithms
Text file
Storage for data that lasts after the program stops. Open it with
new Scanner(new File("data.txt")).Topic 4.6: Using Text Files
throws IOExceptionAdded to the header of a method that opens a file, saying what happens if it can't be opened: the program stops.
Topic 4.6: Using Text Files
hasNext()Returns
trueif there's another item to read.while (input.hasNext())reads a file until it's used up.Topic 4.6: Using Text Files
Scannerreading methodsnextInt(),nextDouble(),nextBoolean()andnext()read one item;nextLine()reads a whole line. Callclose()when done.Topic 4.6: Using Text Files
InputMismatchExceptionThrown when
nextInt,nextDoubleornextBooleanreads an item of the wrong type, like the wordx.Topic 4.6: Using Text Files
splitline.split(",")returns aStringarray of the pieces between commas:"3,4".split(",")is{"3", "4"}.Topic 4.6: Using Text Files
Wrapper class
IntegerandDoubleturn primitive values into immutable objects, so they can go in anArrayList.Topic 4.7: Wrapper Classes
Autoboxing and unboxing
Java converts automatically:
Integer n = 5;boxes anint, andint k = n;unboxes it back.Topic 4.7: Wrapper Classes
Integer.parseIntandDouble.parseDoubleTurn a
Stringinto a number:Integer.parseInt("42")is 42 andDouble.parseDouble("2.5")is 2.5.Topic 4.7: Wrapper Classes
ArrayList<E>A resizable list of objects from
java.util.Eis the element type, likeArrayList<String>; useInteger, notint.Topic 4.8: ArrayList Methods
add(index, obj)Inserts at
index(0 tosize()) and shifts later elements right.add(obj)adds to the end.Topic 4.8: ArrayList Methods
remove(index)Removes and returns the element at
index, shifting later elements left, so their indexes drop by 1.Topic 4.8: ArrayList Methods
getandsetget(i)returns the element ati.set(i, obj)replaces it and returns the old element; the size doesn't change.Topic 4.8: ArrayList Methods
Removing during a traversal
Removing while looping forward skips the next element. Loop backward, or don't add 1 to
iafter a removal.Topic 4.9: ArrayList Traversals
ConcurrentModificationExceptionCan be thrown when an
ArrayListchanges size inside an enhancedforloop over it. Use an indexed loop to add or remove.Topic 4.9: ArrayList Traversals
IndexOutOfBoundsExceptionThrown when an
ArrayListindex is out of range, likelist.get(list.size()).Topic 4.9: ArrayList Traversals
Parallel traversal
Using one index to walk through two lists or arrays at once, stopping at the end of the shorter one.
Topic 4.10: Implementing ArrayList Algorithms
2D array
An array of arrays:
g = new int[3][4]has 3 rows and 4 columns.g[r][c]is one element andg[r]is a whole row.Topic 4.11: 2D Array Creation and Access
Row-major order
Visiting a 2D array across each row before moving down: the outer loop goes over rows and the inner loop over columns.
Topic 4.12: 2D Array Traversals
Column-major order
Visiting a 2D array down each column before moving right: the outer loop goes over columns and the inner loop over rows.
Topic 4.12: 2D Array Traversals
Linear search
Checks elements one at a time, from either end, until it finds the target (return its index) or runs out (return -1).
Topic 4.14: Searching Algorithms
Selection sort
Each pass finds the smallest remaining value and swaps it into its final place.
Topic 4.15: Sorting Algorithms
Insertion sort
Each pass takes the next value and shifts larger sorted values right to slide it into place. Its spot isn't final yet.
Topic 4.15: Sorting Algorithms
Recursion
A method that calls itself. It needs a base case that stops it and recursive calls that move toward that base case.
Topic 4.16: Recursion
Base case
The case where a recursive method returns without calling itself. Without reaching one, the calls never stop.
Topic 4.16: Recursion
Binary search
On sorted data, check the middle and discard the half that can't hold the target. At most 10 checks are needed for 1,000 values.
Topic 4.17: Recursive Searching and Sorting
Merge sort
A recursive sort: split the list in half, sort each half, then merge the two sorted halves into one.
Topic 4.17: Recursive Searching and Sorting