Welcome to the World of Arrays!

Up until now, you’ve been working with single variables—one name for one piece of data. But what if you needed to track the test scores of 30 students? Creating 30 different variables like score1, score2, and so on, would be a nightmare! This is where Arrays come to the rescue. In this chapter, we will learn how to create a single container that can hold many values of the same type and how to grab exactly the piece of information you need from it.

What exactly is an Array?

An array is a collection of data items of the same type. Think of an array like a row of school lockers. Each locker is the same size, they are all lined up in a specific order, and each one has a specific number (an index) so you can find it easily.
Important Note: In Java, once you create an array, its size is fixed. You can't add a "31st locker" to a row of 30 without building a whole new row!

Step 1: Declaring and Creating an Array

To use an array, you have to tell Java two things: what type of data it will hold and how many items it will store. We use the keyword new because an array is actually an object in Java.

The Syntax

To declare and create an array of integers with 5 spaces, you would write:
int[] highScores = new int[5];

Let's break that down:

  1. int[]: This tells Java "I am making an array of integers." The square brackets [] are the secret code for "array."
  2. highScores: This is just the name of your variable.
  3. new int[5]: This actually sets aside space in the computer's memory for \( 5 \) integers.

Did you know? You can also create an array and fill it with values immediately using an initializer list:
int[] primeNumbers = {2, 3, 5, 7, 11};
In this case, Java is smart enough to see there are \( 5 \) numbers and sets the size automatically.

Key Takeaway

An array can only hold one type of data. You can have an array of int, double, or boolean, but you can't mix them together in the same array!

Step 2: Accessing Elements (The "Zero" Rule)

To get a value out of an array or change a value, you use its index. However, Java uses zero-based indexing. This is the part that trips up almost everyone at first!

Imagine your array has \( 5 \) slots. In the real world, we count 1, 2, 3, 4, 5. In Java, we count:
\( 0, 1, 2, 3, 4 \)

  • The first element is at index \( 0 \).
  • The last element is always at index \( length - 1 \).

Example:
highScores[0] = 95; // This sets the very first spot to 95
int myScore = highScores[0]; // This looks at the first spot and gives you the value

Step 3: Default Values

When you create an array using the new keyword but don't put any numbers in it yet, Java doesn't leave them empty. It fills them with default values based on the data type:

  • int arrays are filled with \( 0 \)
  • double arrays are filled with \( 0.0 \)
  • boolean arrays are filled with false
  • Arrays of objects (like String) are filled with null

Don't worry if this seems tricky! Just remember that Java likes to be tidy and ensures every "locker" has a default starting value.

Common Mistake: The "Out of Bounds" Error

This is the most common error in AP Computer Science A! It's called the ArrayIndexOutOfBoundsException.
If you have an array of size \( 10 \), the valid indices are \( 0 \) through \( 9 \). If you try to access myArray[10], your program will crash because there is no locker number 10!
Memory Trick: Always think "Size minus one" for the last element!

Summary of Array Basics

1. Declaration: Use type[] name.
2. Creation: Use the new keyword and specify the size.
3. Fixed Size: You cannot change the size of an array after it is created.
4. Indexing: Starts at \( 0 \) and ends at \( length - 1 \).
5. Access: Use name[index] to get or set a value.

Note: In the next chapters, we will learn how to use loops to "traverse" through an entire array (Unit 4.4) and how to use more flexible lists called ArrayLists (Unit 4.8).