Verified for the 2025 AP Computer Science A exam•Last Updated on June 18, 2024
Arrays help programmers organize lots of related data without creating separate variables for each piece of information. Think of arrays like a row of labeled boxes where you can store similar items together—making your code cleaner and easier to work with.
An array is like a collection of numbered storage containers that all hold the same type of item. Imagine a row of identical lockers, each with its own number (starting at 0), and each storing one item of the same type.
Here are the main ways to make arrays:
// First, say you want an array of numbers
int[] numbers;
// Then, create an array with 5 empty spots
numbers = new int[5];
// Create and set up an array with 10 spots in one step
int[] scores = new int[10];
// Create an array already filled with specific values
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
When you create a new array using new
, Java automatically fills it with "starter" values:
Data Type | Default Value | What It Means |
---|---|---|
int | 0 | Zero |
double | 0.0 | Zero |
boolean | false | Not true |
Objects (like String) | null | Empty - no object |
int[] nums = new int[3];
// nums contains: [0, 0, 0]
double[] measurements = new double[2];
// measurements contains: [0.0, 0.0]
boolean[] flags = new boolean[2];
// flags contains: [false, false]
String[] names = new String[2];
// names contains: [null, null] (null means "nothing here yet")
To get or change items in your array, use square brackets []
with the position number:
// Create an array with 5 spots
int[] values = new int[5];
// Put values into specific positions
values[0] = 10; // First position (remember, we start at 0!)
values[1] = 20; // Second position
values[4] = 50; // Last position
// Get values from positions
System.out.println(values[2]); // Prints 0 (the default value)
Valid positions go from 0 to (array length - 1)
If you try to access a position that doesn't exist, you'll get an error called ArrayIndexOutOfBoundsException - this is Java's way of telling you that you're trying to use a position number that's outside the valid range of the array
int[] data = new int[3]; // Creates array with positions 0, 1, and 2
// These work fine
data[0] = 100;
data[2] = 300;
// These will crash your program
// data[-1] = 50; // There's no negative position
// data[3] = 400; // Position 3 doesn't exist in a 3-item array
Use the length
property to see how many spots are in an array:
int[] scores = new int[25];
System.out.println(scores.length); // Prints: 25
// Using a regular for loop with position numbers
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Item at position " + i + ": " + numbers[i]);
}
// Using enhanced for loop (easier but doesn't give you the position)
for (int num : numbers) {
System.out.println("Value: " + num);
}
Let's see what happens with this code:
int[] values = new int[4];
values[0] = 10;
values[1] = 20;
values[3] = 40;
Here's what the array looks like in memory:
Position | 0 | 1 | 2 | 3 |
---|---|---|---|---|
Value | 10 | 20 | 0 | 40 |
Notice position 2 still has the default value (0) because we never put anything there!
Forgetting arrays start at 0: The first position is 0, not 1
int[] nums = new int[5]; // Valid positions: 0,1,2,3,4
Trying to access positions that don't exist (causes ArrayIndexOutOfBoundsException):
int[] values = new int[3];
values[3] = 10; // Error! Valid positions are only 0,1,2
Using an array you created but never initialized:
int[] data; // Said we want an array, but didn't create it
data[0] = 5; // Error! The array doesn't actually exist yet
Arrays let you store many related values together using a single variable name. We've learned how to create arrays, put values in them, and get values out using position numbers (indices). Remember that arrays have a fixed size that can't change, positions start counting at 0, and you'll get errors if you try to access positions outside the array's valid range.