Arrays
An array holds a fixed number of values of the same type, stored in order and accessed by index: a week of temperatures, a list of scores, the pixels of an image. Arrays are Java's most basic collection and the building block underneath many others.
Creating arrays
void main() { int[] scores = {72, 88, 95, 61}; String[] days = new String[3]; days[0] = "Mon"; days[1] = "Tue"; System.out.println(scores[2]); System.out.println(scores.length); System.out.println(days[1] + " " + days[2]); }
95 4 Tue null
Either list the values in braces, or use new Type[size], which fills the array with default values (0, false, or null for objects). length is a field, not a method — no parentheses.
Changing elements
void main() { int[] stock = {5, 0, 12}; stock[1] = 20; stock[2] += 3; System.out.println(stock[0] + ", " + stock[1] + ", " + stock[2]); }
5, 20, 15
An array's size is fixed when it's created; to grow, create a bigger array and copy — or use an ArrayList (lesson 40).
Looping over arrays
void main() { double[] temps = {18.5, 21.0, 19.5, 24.0, 22.5}; double sum = 0; double max = temps[0]; for (double t : temps) { sum += t; if (t > max) max = t; } System.out.println("Average: " + sum / temps.length); System.out.println("Max: " + max); }
Average: 21.1 Max: 24.0
Printing an array
import java.util.Arrays; void main() { int[] numbers = {3, 1, 2}; System.out.println(numbers.length > 0); System.out.println(Arrays.toString(numbers)); }
true [3, 1, 2]
Printing an array directly shows something like [I@1b6d3586 (its type and a hash code). Arrays.toString shows the contents.
The Arrays utility class
import java.util.Arrays; void main() { int[] values = {42, 7, 19, 3, 25}; Arrays.sort(values); System.out.println(Arrays.toString(values)); System.out.println(Arrays.binarySearch(values, 19)); int[] copy = Arrays.copyOf(values, 7); System.out.println(Arrays.toString(copy)); int[] filled = new int[4]; Arrays.fill(filled, 9); System.out.println(Arrays.toString(filled)); System.out.println(Arrays.equals(new int[]{1, 2}, new int[]{1, 2})); }
[3, 7, 19, 25, 42] 2 [3, 7, 19, 25, 42, 0, 0] [9, 9, 9, 9] true
Arrays sorts, searches (in a sorted array), copies, fills and compares. Note Arrays.equals: == on arrays only checks whether they're the same object.
Arrays are references
import java.util.Arrays; void main() { int[] original = {1, 2, 3}; int[] alias = original; int[] copy = original.clone(); alias[0] = 99; System.out.println(Arrays.toString(original)); System.out.println(Arrays.toString(copy)); }
[99, 2, 3] [1, 2, 3]
An array variable holds a reference. Assigning it to another variable doesn't copy the array — both names point to the same one. Use clone() or Arrays.copyOf for a real copy.
Index out of bounds
void main() { String[] sizes = {"S", "M", "L"}; System.out.println(sizes[3]); }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at Main.main(Main.java:3)Java checks every index. Going outside the array is an error rather than silently reading random memory, as in some older languages.
Try it yourself
Count how many values in an array are above the average.
Average 224.2, 2 days above average