Arrays: fixed-size, indexed containers.
When you need to store many values of the same type and reach any one of them instantly, an array is the right tool.
An array is an object that stores a fixed number of values of the same type, arranged in a numbered sequence. You reach individual elements with an integer index, starting from 0.
Arrays are reference types: an array variable holds a reference to the array object in memory. Their size is fixed when they are created and cannot change afterward.
On the AP exam, arrays appear in tracing MCQs, in array-manipulation FRQs, and as the underlying structure behind sorting and searching algorithms.
Declare, create, index, measure.
Four operations that cover almost every array task.
Declaring and creating
int[] scores = new int[5]; // 5 ints, all 0 by default
String[] names = new String[3]; // 3 references, all null
int[] primes = {2, 3, 5, 7, 11}; // initializer list
The new form creates an array of the given size, filled with default values. The initializer list creates and fills the array in one step.
Default values
- Numeric types →
0or0.0 boolean→false- Object types →
null
Indexing
int first = primes[0]; // 2
int last = primes[4]; // 11
primes[2] = 50; // replace element at index 2
Indices start at 0 and go up to length - 1. An invalid index throws ArrayIndexOutOfBoundsException at runtime.
Length
int n = primes.length; // 5 (no parentheses!)
length is a field, not a method. There are no parentheses. This is different from String's length() method.
Arrays are references
int[] a = {1, 2, 3};
int[] b = a; // b refers to the SAME array as a
b[0] = 99;
System.out.println(a[0]); // 99
Assignment copies the reference, not the contents. To copy elements, you must loop or use a helper.
Array vocabulary every AP question uses.
Tight terminology speeds up MCQ reading.
- Array — a fixed-size sequence of values of the same type.
- Element — a single value stored in an array.
- Index — the integer position of an element, starting at
0. - Length — the number of slots in an array; accessed as
arr.length. - Initializer list — the
{ ... }syntax that creates an array with explicit values. - Reference type — array variables hold references, not the values themselves.
- ArrayIndexOutOfBoundsException — runtime error from an invalid index.
How arrays appear on the AP exam.
Bounds, aliasing, and length confusion are the top traps.
MCQ patterns to expect:
lengthvslength(). Arrays use the fieldarr.length; Strings use the methods.length().- Off-by-one bounds. Valid indices are
0toarr.length - 1.arr[arr.length]throws an exception. - Aliasing. Assigning one array variable to another shares the same array object.
- Default values. A newly created
int[]is full of zeros, not garbage.
FRQ tip: always use arr.length as the bound — never a hard-coded number.
Trace array aliasing carefully.
A single line of assignment can change two variables' visible contents.
Problem. What is printed?
int[] a = {10, 20, 30, 40};
int[] b = a;
int[] c = {10, 20, 30, 40};
b[1] = 99;
System.out.println(a[1] + " " + b[1] + " " + c[1]);
Step 1. a and b reference the same array object. c references a different array with the same values.
Step 2. b[1] = 99 changes the shared object at index 1.
Step 3. a[1] and b[1] both read the same array's element 1, which is now 99. c[1] reads from the separate array, still 20.
Answer. The program prints 99 99 20.
Why this matters. Arrays are objects. Assignment never copies their contents — only the reference. To actually duplicate an array, you must build a new one and copy each element with a loop.
Array errors that keep showing up.
The same handful of mistakes account for most lost points.
- Using
arr.length(). Arrays have nolength()method. Usearr.length(no parentheses). - Index
arr.length. ThrowsArrayIndexOutOfBoundsException. The last valid index isarr.length - 1. - Negative index. Also throws the same exception.
- Resizing an array. Impossible. Once created, an array's length is fixed. Build a new one if you need a different size.
- Assuming assignment copies.
b = a;aliases; it does not duplicate. - Forgetting
new.int[] arr;declares a reference, but no array exists yet. Reading it throwsNullPointerException.
Array essentials at a glance.
A quick lookup for syntax and behavior.
Arrays Reference
AP Quick Reference| Operation | Syntax | AP Exam Tip |
|---|---|---|
| Declare and size | int[] a = new int[5]; |
All elements default to 0. |
| Initializer list | int[] a = {1, 2, 3}; |
Compact way to set values up front. |
| Access an element | a[i] |
Indices run from 0 to length - 1. |
| Number of elements | a.length |
Field, not method — no parentheses. |
| Modify an element | a[i] = value; |
Old value is replaced. |
| Aliasing | int[] b = a; |
Both refer to the same array. |
How to lock in array fundamentals.
Master the basics before tackling traversal patterns.
Use the three practice tools below this lesson in order:
- MCQ Practice — for every array, write the indices above each element on paper. This removes bounds confusion and aliasing surprises.
- Java Lab — create, fill, read, and modify arrays of
int,double, andString. Practise bothnewand initializer-list creation. - FRQ Practice — always use
arr.lengthas your loop bound. Never assume a length; let the array tell you.
If you can sketch the array, indices, and references on paper, you'll never miss a basic array MCQ.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.