6m left·0%
Reading Time: 6 min
Last Updated: August 10, 2026
Main Ideas: 5
Reading Time: 6 min
Last Updated: August 10, 2026
Main Ideas: 5

Topic 4.3 Notes – Array Creation and Access

Verified for 2027 AP® Computer Science A Exam
Read aloud
Arrays let you store many values of the same type under one variable name and work with them using index numbers. This topic focuses on how arrays are built, how they’re initialized, and how to safely access their elements.

1. What a 1D Array Is

A 1D array is a single variable that holds multiple values of the same data type.

  • All elements must be:
    • primitive values (int, double, boolean), or
    • object references (String, Student, etc.)
  • Each stored value is called an element.
  • Each element has a numbered position called an index.
  • Arrays use zero-based indexing. The first element is at index 0.

Picture an array like this:

1D array with zero-based indexing

One variable name → many indexed slots.

Arrays are objects

Even though they feel different from other objects, arrays:

  • Are created using new
  • Have a built-in length attribute
  • Store data in memory like any object

Fixed size

The length is set when the array is created and cannot change.

If you create an array with 6 elements, it will always have 6 elements. You can change the values inside, but not the size.

Key vocabulary:

  • Element → value inside the array
  • Index → position number
  • Length → total number of elements

2. Declaring and Creating Arrays

There are two separate ideas here: declaring the variable and creating the array.

Declaration

int[] numbers;
String[] words;

This only creates a reference variable. No array exists yet.

If you try to use it now, you’ll get a NullPointerException.

Creation with new

numbers = new int[4];

Now the array object exists.

  • Length is permanently 4
  • Valid indices are 0, 1, 2, 3

Default Values

When you use new, Java automatically fills the array:

  • int → 0
  • double → 0.0
  • boolean → false
  • Object references → null

Example:

String[] names = new String[3];

All three elements currently store null. The array exists, but no String objects are inside yet.

That distinction shows up a lot on quizzes.

Initializer Lists

If you already know the values:

int[] ages = {15, 16, 15, 17};
String[] colors = {"red", "blue", "green"};
  • Java figures out the length automatically.
  • You don’t write new int[...] in this form.

You’ll see both styles in multiple choice.

3. Accessing and Modifying Elements

Use square brackets with an index:

ages[1] = 18;       // modify
int x = ages[2];    // access

Same syntax for reading and writing.

Index Rules

Valid index values are:

0 through array.length - 1

In math form:

0 ≤ index < array.length

If array.length is 5, valid indices are 0-4.

The length Attribute

Every array has:

array.length

Notice:

  • No parentheses
  • It is an attribute, not a method

Common loop pattern:

for (int i = 0; i < array.length; i++) {
    // process array[i]
}

The < is critical.

4. Index Errors and How to Avoid Them

ArrayIndexOutOfBoundsException

This happens if the index is:

  • Negative
  • Equal to length
  • Greater than length - 1

Example mistake:

for (int i = 0; i <= array.length; i++)

That <= will crash when i == array.length.

Most AP questions that involve arrays are testing whether you understand that the last valid index is length - 1.

NullPointerException with Arrays

There are two different null problems.

1. The array reference is null

int[] nums;
nums[0] = 5;   // crash

The array was declared but never created.

2. The elements are null

String[] names = new String[2];
int len = names[0].length();  // crash

The array exists, but names[0] is null.

On multiple choice, they love mixing these up.

5. How This Appears on the AP Exam

Arrays show up constantly.

You’ll be asked to:

  • Trace loops that use array.length
  • Identify which line causes an exception
  • Write methods that process arrays
  • Use arrays as instance variables in classes

If you see array code, immediately:

  1. Identify the length.
  2. Determine valid index range.
  3. Watch for <=.
  4. Check for null references.

Arrays are the foundation for 2D arrays later. Everything about indexing and length carries forward.

Key Takeaways

Arrays store multiple values of the same type under one variable name.
The length is fixed at creation and cannot change.
When created with new, elements get default values (0, 0.0, false, or null).
Valid indices are always 0 through length - 1.
array.length is an attribute, not a method.
Using <= array.length in a loop is one of the fastest ways to lose points.
A declared array variable is null until you use new.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse this website.

Notes

1 credit used · 5/5 remaining