01 / 20
3 7 1 9 5

Array Creation
and Access

Master the fundamentals of declaring, initializing, and accessing arrays in Java: the building blocks of organized data.

AP Computer Science A

What Is an Array?

An array is a fixed-size, ordered collection of elements that all share the same data type. Think of it as a row of numbered boxes: each box holds one value, and you locate any box by its index.

42 17 93 8 55 [0] [1] [2] [3] [4] indices (zero-based)

Why Use Arrays?

Organized Storage
Store hundreds of related values under a single variable name instead of declaring separate variables.
Fast Access
Jump directly to any element by its index in constant time: no searching required.
Loop-Friendly
Process every element with a simple loop, making repetitive operations concise and powerful.

Arrays in Memory

In Java, an array is an object stored on the heap. The variable you declare holds a reference (a pointer) to the actual data. This reference model means arrays are passed by reference to methods.

scores Stack (reference) Heap (array object) 90 85 72 98 67 [0] [1] [2] [3] [4] length = 5

Declaring an Array

Java offers two syntaxes for array declaration. Both are valid, but the first form is preferred in AP CS A because the brackets clarify the type.

// Preferred: brackets with the type
int[] scores;
String[] names;

// Also valid: brackets with the variable
int scores[];
String names[];

Two Ways to Create an Array

Using new
int[] arr = new int[5];
// Creates 5 slots,
// all initialized to 0
Initializer List
int[] arr = {3, 7, 1, 9, 5};
// Creates + fills in
// one step

Default Values by Type

Element Type Default Value Example
int, short, byte, long 0 new int[3] → {0, 0, 0}
double, float 0.0 new double[2] → {0.0, 0.0}
boolean false new boolean[2] → {false, false}
String (or any object) null new String[2] → {null, null}

Accessing Elements

Use the array name followed by square brackets containing the index. You can both read and write with this syntax.

int[] nums = {10, 20, 30, 40};

// Read: get element at index 2
int val = nums[2]; // val = 30

// Write: set element at index 0
nums[0] = 99; // nums is now {99, 20, 30, 40}

.length vs .length() vs .size()

Feature Array String ArrayList
Syntax arr.length str.length() list.size()
Type Field (property) Method call Method call
Parentheses? No Yes Yes
Resizable? No No Yes

Bounds Checking

Java checks every array access at runtime. If you use an index \(< 0\) or \(\ge .length\), the JVM throws an ArrayIndexOutOfBoundsException.

Valid indices: 0 through \(length - 1\)
A B C D E [0] [1] [2] [3] [4] [-1] [5] CRASH CRASH length = 5

Step-by-Step Approach

1
Choose your type. Decide the element type: int, double, String, etc.
2
Declare the array. Write type[] name; to create the reference variable.
3
Initialize. Use new type[size] for default values, or {val1, val2, ...} for specific values.
4
Access elements. Use name[index] to read or write. Remember: indices go from \(0\) to \(length - 1\).
5
Traverse with a loop. Use a for loop or for-each loop to process all elements.

Example 1: Creating and Reading

double[] temps = {72.5, 68.3, 75.1, 69.8};

System.out.println(temps[0]); // 72.5
System.out.println(temps[3]); // 69.8
System.out.println(temps.length); // 4
72.5 68.3 75.1 69.8 [0] ← read [1] [2] [3] ← read

Example 2: Modifying Elements

String[] colors = new String[3];
// colors = {null, null, null}

colors[0] = "Red";
colors[1] = "Blue";
colors[2] = "Green";

// Overwrite index 1
colors[1] = "Yellow";
// colors = {"Red", "Yellow", "Green"}

Example 3: Traversing an Array

int[] nums = {5, 12, 8, 3, 17};
int sum = 0;

for (int i = 0; i < nums.length; i++) {
  sum += nums[i];
}

System.out.println(sum); // 45

The Enhanced For Loop

int[] nums = {5, 12, 8, 3, 17};

for (int n : nums) {
  System.out.print(n + " ");
}
// Output: 5 12 8 3 17
Use When
You need to read every element and don't need the index.
Avoid When
You need to modify elements, skip indices, or traverse backwards.

Common Mistakes

✕ Off-by-One
Using \(\le\) instead of \(<\) in loop condition: \(i \le arr.length\) crashes on the last iteration.
✕ Wrong .length
Writing arr.length() with parentheses. Arrays use a field, not a method.
✕ Uninitialized Array
Declaring int[] arr; then accessing \(arr[0]\) without new. The reference is null.

Question 1 of 5

What is the output of the following code?

int[] a = {4, 8, 15, 16, 23};
System.out.println(a[2]);

Question 2 of 5

Which line of code correctly creates an array of 5 doubles, all initialized to 0.0?

Question 3 of 5

What happens when you execute arr[arr.length] on an array of size 5?

Question 4 of 5

What is the default value of each element in new String[3]?

Question 5 of 5

Consider the following code. What is the value of result?

int[] vals = {2, 4, 6, 8};
vals[1] = vals[3];
int result = vals[0] + vals[1];