← All lessons
Java basics · Lesson 7 of 10

Arrays

A variable holds one value. An array holds a row of values of the same type under one name, like a row of numbered lockers. You make one by listing the values in curly brackets, with [] after the type:

Java
int[] scores = {90, 75, 82};
String[] names = {"Ana", "Ben", "Cy"};
System.out.println(scores[0]);
System.out.println(names[2]);
System.out.println(scores.length);
Prints
90
Cy
3

Each item is reached by its index in square brackets, counting from 0 like the characters of a string. So the first item is scores[0] and the last is scores[scores.length - 1]. length (no brackets for arrays) is how many items there are.

You can change an item by storing into its index. You can't make an array longer or shorter, though: its size is fixed when it's made. new int[5] makes an array of five zeros to fill in later.

Java
int[] days = new int[5];
days[0] = 8;
days[4] = 3;
days[0] = days[0] + 1;
System.out.println(Arrays.toString(days));
Prints
[9, 0, 0, 0, 3]

Arrays.toString turns an array into readable text. Printing an array directly shows something unhelpful like [I@6d06d69c, which is Java's way of naming the array object, not its contents. (Arrays needs import java.util.Arrays; at the top of the file; the examples here include it for you.)

Asking for an index that doesn't exist, like days[5] in a five-item array, stops the program with an ArrayIndexOutOfBoundsException. That's an off-by-one mistake, the most common bug with arrays: the last index is one less than the length.

Looping over an array

Arrays and loops go together. A counting loop from 0 to length - 1 visits every index; the for-each loop, for (int s : scores), reads "for each s in scores" and hands you each item in turn when you don't need the index.

Java
int[] scores = {90, 75, 82};
for (int i = 0; i < scores.length; i++) {
    System.out.println("Test " + (i + 1) + ": " + scores[i]);
}
int sum = 0;
for (int s : scores) {
    sum += s;
}
System.out.println("Average: " + sum / scores.length);
Prints
Test 1: 90
Test 2: 75
Test 3: 82
Average: 82

Finding the biggest item is another classic: start with the first one as the best so far, then look at each of the rest and keep whichever is bigger.

Java
int[] temps = {12, 19, 7, 23, 15};
int highest = temps[0];
for (int t : temps) {
    if (t > highest) highest = t;
}
System.out.println(highest);
Arrays.sort(temps);
System.out.println(Arrays.toString(temps));
Prints
23
[7, 12, 15, 19, 23]

Arrays.sort puts the items in order, smallest first. (An if with a single line after it can skip the curly brackets, as above, but only for one line.)

When you need a list that grows and shrinks, Java has ArrayList, which the Intermediate Java course covers. Arrays are the simple, fixed-size building block underneath.

Practice

What does this print?

int[] a = {4, 8, 15, 16};
System.out.println(a[1]);
More info

Quick reference

int[] a = {1, 2, 3};An array of ints with three items.
new int[n]An array of n zeros.
a[0] / a[a.length - 1]The first / the last item.
a.lengthHow many items (no brackets).
for (int x : a) { }For each item in a.
Arrays.toString(a)The items as text, for printing.
Arrays.sort(a)Put the items in order, smallest first.
ArrayIndexOutOfBoundsExceptionAn index past the end (or below 0).

Related courses