53 flashcard terms for AP Computer Science A Unit 6, written to match the course framework. Read them here, drill them as flashcards, or take the 43-question quiz. Free, no account needed.
Arrays fixed-size collections; indexed 0 to length-1. 2D arrays for tables/matrices. Algorithms: search, sort. ArrayList more flexible.
Array Length is Fixed
Once created, an array's length cannot change. To 'grow', create a new larger array and copy elements over. length is a field (no parentheses), unlike String's length().
Array Default Values
new int[5] holds five 0s, new double[3] holds 0.0s, new boolean[2] holds falses, and new String[4] holds four nulls. Calling a method on an unfilled String slot throws NullPointerException.
Array Initializer List
int[] a = {3, 1, 4}; sets length and contents at once. Can only be used in a declaration, not in a later assignment.
ArrayIndexOutOfBoundsException
Thrown for any index below 0 or at/above length. The classic cause is i <= arr.length in a loop header.
Enhanced for Loop Semantics
for (int x : arr) copies each element into x. Assigning to x does not change the array; for arrays of objects, calling mutators on x does change the shared object.
Enhanced for Limitations
No index is available and elements cannot be replaced or the array resized. Use a standard for loop when you need indices, need to modify primitives, or traverse in reverse.
Array Aliasing
int[] b = a; makes b refer to the same array; b[0] = 9 changes a[0]. Copying requires a loop or creating a new array.
Passing an array passes its reference; the method can modify the caller's elements. Reassigning the parameter to a new array does not affect the caller.
Sequential Search on an Array
Scan indices from 0 to length - 1 and return the index of the first match, or -1 if none. Works on unsorted data; runs up to n comparisons.
Reversing an Array In Place
Swap arr[i] with arr[length - 1 - i] for i from 0 to length / 2 - 1. Going past the midpoint would swap everything back.
Shifting Elements
To insert at index k, move elements k..length-2 one slot right starting from the END; to delete, move elements k+1..length-1 one slot left starting from k.
Rotating an Array
Rotating left by one: save arr[0], shift everything left, put the saved value at the end. Rotating right is the mirror operation.
Consecutive-pair Traversal
To compare neighbors, loop i from 0 to length - 2 and use arr[i] and arr[i + 1]. Looping to length - 1 with arr[i + 1] overruns the array.
Two-index Traversal
Some algorithms use i from the front and j from the back moving toward each other, e.g. checking a palindrome or partitioning values.
Counting and Frequency
int[] freq = new int[10]; freq[digit]++ counts occurrences of each digit; the value being counted is used as the index.
Average Requires Double Division
sum / arr.length truncates if both are ints. Cast one operand or declare sum as double for a correct average.
Array of Objects
Student[] roster = new Student[30]; creates 30 null slots; each must be assigned a new Student before its methods are called.
Array Length vs Last Index
For length n the last valid index is n - 1. Many bugs come from using n where n - 1 is meant, especially in reverse loops starting at arr.length.
Detecting a Property of All Elements
Start boolean all = true and set false when a counterexample appears; for 'any element' start false and set true on a match. Do not reset the flag inside the loop.
Traversing in Reverse
for (int i = arr.length - 1; i >= 0; i--) visits elements last to first, needed when deleting while traversing or shifting right.
Array Object Identity
An array is an object, so two arrays with identical contents are still different objects and == compares references rather than elements.
Copying Requires a Loop
int[] b = a; aliases the same array; a genuine copy needs a new array and an element-by-element loop.
Enhanced for Cannot Write Back
Assigning to the loop variable of an enhanced for changes only the copy, so element modification requires an indexed loop.
Enhanced for on Object Arrays
The loop variable holds a reference, so calling a mutator on it does change the stored object even though assignment would not.
In-place Reverse
Swap a[i] with a[n - 1 - i] for i below n / 2; running the loop to n would undo every swap.