4. Data Collections

Array Creation And Access

Array Creation and Access

students, imagine you are organizing a school event with hundreds of student sign-ups 📋. You need a way to store names, keep them in order, and quickly find a specific person. In AP Computer Science A, one of the most important tools for this job is a collection. Today’s lesson focuses on array creation and access, which is the starting point for working with many data collections in Java.

What You Will Learn

By the end of this lesson, students, you should be able to:

  • Explain what an array is and why programmers use it.
  • Create arrays in Java using the correct syntax.
  • Access and update array elements using indexes.
  • Predict the values stored in an array after code runs.
  • Connect arrays to the larger AP CSA topic of data collections.

Arrays are one of the first collection types students learn because they are simple, fast, and foundational. They also help build understanding for later topics such as $2$D arrays, searching, sorting, and recursion.

What Is an Array?

An array is a data structure that stores multiple values of the same type in a single variable. Instead of creating separate variables like score1, score2, and score3, you can store all the values in one array. This is useful when the data belongs together.

For example, if a teacher stores quiz scores for a class, an array can hold all the scores at once:

int[] scores = {88, 92, 75, 100};

This array has four elements. Each element is an int, and the array’s type is int[].

A key idea in arrays is that elements are stored in order. That order matters, because each position has an index.

Important terminology

  • Array: a collection of values of the same type
  • Element: one value stored in the array
  • Index: the position of an element in the array
  • Length: the number of elements in the array

In Java, array indexes begin at $0$. That means the first element is at index $0$, the second is at index $1$, and so on. For the array scores, the value 88 is at index $0$, 92 is at index $1$, 75 is at index $2$, and 100 is at index $3$.

This zero-based indexing is one of the most important facts to remember. A common mistake is to think the first element is at index $1$. It is not. 🚦

Creating Arrays in Java

There are two common ways to create arrays in AP Computer Science A.

1. Create and initialize at the same time

You can declare an array and provide values immediately:

String[] names = {"Ana", "Ben", "Carla"};

This creates a String array with three elements.

2. Create an empty array with a fixed size

You can also create an array by giving its length:

double[] temperatures = new double[7];

This creates an array with $7$ elements. Because it is a double array, each element is automatically initialized to $0.0$.

Java always gives arrays a fixed size after creation. That means the number of elements does not change. If you need a collection that can grow or shrink, that is when ArrayList becomes useful later in the course.

Default values

When an array is created with new, each element starts with a default value:

  • int arrays: $0$
  • double arrays: $0.0$
  • boolean arrays: false
  • object arrays such as String[]: null

Example:

boolean[] choices = new boolean[3];

The array choices contains three values, all set to false at first.

Knowing the default value helps you predict behavior in code. For example, if an array element has not been changed yet, it still holds its default value.

Accessing Array Elements

To access one element in an array, use square brackets and an index.

int[] scores = {88, 92, 75, 100};
System.out.println(scores[2]);

This prints $75$ because scores[2] refers to the element at index $2$.

If you want to change an element, you use the same syntax on the left side of an assignment statement:

scores[1] = 95;

Now the second element changes from $92$ to $95$.

Reading code carefully

Suppose we have:

String[] teams = {"Red", "Blue", "Green"};
teams[0] = "Gold";
System.out.println(teams[0]);

The output is Gold because the first element was replaced. This is a common AP CSA style question: you must trace the code step by step.

Index out of bounds

An array index must be between $0$ and array.length - 1. If you try to access scores[4] in an array of length $4$, Java throws an error because the valid indexes are $0$, $1$, $2$, and $3$.

This is called an ArrayIndexOutOfBoundsException. It happens when code tries to access a position that does not exist.

For example:

int[] nums = {5, 10, 15};
System.out.println(nums[3]);

This is invalid because the last element is at index $2$, not $3$.

Using the Length of an Array

Every array has a built-in field called length. It tells you how many elements the array contains.

int[] data = {4, 8, 12, 16};
System.out.println(data.length);

This prints $4$.

Notice that length does not have parentheses. It is a field, not a method.

A very common pattern in AP CSA is using length in a loop:

for (int i = 0; i < data.length; i++) {
    System.out.println(data[i]);
}

This loop starts at index $0$ and goes through the final valid index, which is data.length - 1. That makes it safe for accessing every element.

Why this matters

If you write i <= data.length, the loop will try to access one index too far. The correct condition is i < data.length.

This detail matters a lot on the AP exam, especially when you are asked to predict output or identify logic errors.

Real-World Example: Tracking Book Checkouts 📚

Imagine a library records the number of books checked out each day for a week:

int[] checkouts = {14, 9, 18, 12, 20, 7, 11};

This array stores seven values, one for each day.

  • checkouts[0] is $14$
  • checkouts[3] is $12$
  • checkouts[6] is $11$

If the library wants to update Thursday’s count, it can do this:

checkouts[4] = 22;

Now the value for Thursday changes to $22$.

Arrays are useful here because the data is related, ordered, and easy to process with loops. That is why they are part of data collections. They help programmers organize many values under one name.

How Array Creation and Access Connects to Data Collections

The topic of Data Collections includes several ways to store and use groups of data. Arrays are the starting point because they teach the main ideas behind collections:

  • storing many values together
  • using indexes to identify elements
  • working with loops to visit each item
  • tracing code carefully to understand behavior

These ideas also appear in ArrayList, 2D arrays, searching, sorting, and recursion. For example, searching through an array means checking elements one by one. Sorting an array means rearranging its elements. Recursion often uses arrays as input data.

Arrays are also important because they show a limitation: their size is fixed after creation. That limitation helps students understand why other collection types exist.

Common Exam Skills With Arrays

On the AP Computer Science A exam, you may be asked to do one or more of these tasks:

  • identify the result of a code segment
  • determine the value of an array element after assignment
  • recognize the correct way to create an array
  • explain why a loop uses array.length
  • find an error caused by an invalid index

Here is a typical reasoning problem:

int[] nums = {2, 4, 6, 8};
nums[2] = nums[0] + nums[1];

After the assignment, nums[2] becomes $2 + 4 = 6$. The array remains {2, 4, 6, 8} in this case, even though the code changed one element, because the new value equals the old value.

A second example:

int[] nums = {3, 6, 9};
nums[1] = nums[2] - nums[0];

Here, nums[1] becomes $9 - 3 = 6$. Again, tracing each step carefully is essential.

Conclusion

Arrays are one of the most important tools in AP Computer Science A because they let programmers store and access many related values using one variable. students, when you understand how to create arrays, read indexes, use length, and avoid invalid positions, you build a strong foundation for the rest of Data Collections. Arrays are not just a topic by themselves; they are the doorway to later ideas like ArrayList, 2D arrays, searching, sorting, and algorithm design. Mastering array creation and access now makes the rest of the course much easier. ✅

Study Notes

  • An array stores multiple values of the same type in one variable.
  • Array indexes start at $0$.
  • The last valid index is array.length - 1.
  • Arrays have a fixed size after creation.
  • Use new Type[size] to create an empty array.
  • Use braces {} to create and initialize an array at the same time.
  • Use array[index] to access or change an element.
  • array.length gives the number of elements.
  • Accessing an invalid index causes an ArrayIndexOutOfBoundsException.
  • Arrays are a core part of Data Collections and help prepare you for ArrayList, 2D arrays, and searching/sorting.

Practice Quiz

5 questions to test your understanding