📖 Crammy · All study guides
AP Computer Science A · Unit 6

Arrays: every key term you need (+ practice quiz)

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.

Study this unit free →

More AP Computer Science A guides

Arrays: Basics
Fixed-size collection of same type. Declaration: int[] arr = new int[5];. Index 0 to length-1.
Array Indexing
Access element: arr[i]. Range 0 to length-1. ArrayIndexOutOfBoundsException if invalid index.
Array Initialization
int[] arr = {1, 2, 3, 4, 5}; or int[] arr = new int[5]; (defaults to 0). Size set at creation.
Array Length
arr.length gives size (not method, property). Used in loops: for (int i=0; i<arr.length; i++).
Arrays as Parameters
Method receives array reference; modifications affect original array. Pass by reference (objects), not value.
Array Algorithms: Search
Linear search: loop through checking each element. Fastest when small. Binary search: requires sorted array; O(log n).
Array Algorithms: Sort
Arrays.sort(arr) built-in. Custom sort needs comparison logic (Comparable interface).
2D Arrays
int[][] matrix = new int[rows][cols];. Jagged arrays: each row different length. Access: matrix[i][j].
2D Array Traversal
Nested loops: for each row, for each column. Standard: row-major (left-to-right within a row, top-to-bottom across rows).
Arrays vs ArrayList
Array: fixed size, primitives allowed, faster. ArrayList: dynamic, objects only, slower. ArrayList wraps primitives (Integer, Double).
Unit 6 Summary
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.
Drill these as interactive flashcards →
Arrays as Parameters
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.
Test yourself on this unit →
Shifting Left Deletes
Copying each element one position down from the deletion point and then clearing the last slot removes an element from a fixed-length array.
Shifting Right Inserts
Traverse from the end downward when opening a gap, or later elements will be overwritten before they are moved.
Rotation by k
newIndex = (i + k) % n places every element in one pass when a second array is available.
Consecutive-pair Loop Bound
Comparing a[i] with a[i + 1] must stop at i < length - 1 or the last comparison reads past the end.
Frequency Counting Array
A counts array indexed by the value itself tallies occurrences in one pass, provided the values fall in a known small range.
Parallel Arrays
Two arrays whose index i describes the same entity must be kept the same length and reordered together, which is why an array of objects is safer.
Search Returns an Index
Returning -1 for absence lets the caller distinguish a miss from a legitimate index 0.
Maximum with Ties
Using a strict > keeps the first maximum, while >= keeps the last; the choice matters when the position is reported.
Average as double
sum / arr.length is integer division; cast one operand or declare sum as double to obtain the true mean.
Array of Objects Starts as null
new Student[5] allocates five null references, so each element must be assigned an object before any method is called on it.
Length Is Final
arr.length is a field, not a method, and cannot be changed; growing requires allocating a larger array and copying.
Bounds Check Order
Test i >= 0 && i < arr.length before indexing, and rely on short-circuit evaluation so the access never runs on a bad index.
Backward Traversal Bound
A reverse loop starts at arr.length - 1 and continues while the index is at least 0, so the terminating comparison is >= rather than >.
Two-array Merge
Advance an index in each sorted source, always copying the smaller front element, until one source is exhausted and the rest is appended.
Sum of Selected Elements
Combine a traversal with a condition to total only the elements satisfying a test, using a separate counter if an average is needed.
Off-by-one at Both Ends
The valid indices run from 0 through length - 1, so both a starting 1 and an ending <= length are classic sources of an exception.
Array Parameters Are References
A method that receives an array can permanently modify the caller's data, which is why sorting methods need no return value.
Turn these into flashcards & quizzes →