4.3 - Array Creation and Access
An array stores multiple values of the same type, either primitives or object references.
int[] scores = new int[5]; // an array that can hold 5 ints
The type in front of the brackets is the type of every element:
int[] is an array of ints, double[] is an array of doubles, and String[] is an array of String references. The number inside the brackets after new is the length.Once you create an array, its length is locked in, you can’t resize it later. You can check the length using the
.length attribute (no parentheses, it’s not a method).System.out.println(scores.length); // 5
If you need more room later, the only option is to make a new, bigger array and copy the values over. Also watch the difference between these three, since they’re easy to mix up: arrays use
length with no parentheses, Strings use the length() method, and ArrayLists (4.8) use the size() method.int[] nums = {4, 8, 15}; String word = "hello"; System.out.println(nums.length); // 3, attribute (no parentheses) System.out.println(word.length()); // 5, method
When you create an array with
new, every element automatically gets set to the default value for that type:Type | Default Value |
int | 0 |
double | 0.0 |
boolean | false |
reference type | null |
int[] nums = new int[3]; // {0, 0, 0}
String[] names = new String[3]; // {null, null, null} boolean[] done = new boolean[2]; // {false, false}
null means that spot doesn’t refer to any object yet. If you call a method on it, like names[0].length(), you get a NullPointerException. You have to store actual objects in the array first.You can also use an initializer list to create and fill an array all at once:
int[] nums = {4, 8, 15, 16, 23};
With an initializer list, Java counts the values to set the length (this one has length 5), and you don’t need
new. It works for any element type:String[] days = {"Mon", "Tue", "Wed"}; // length 3 double[] temps = {98.6, 99.1}; // length 2
Square brackets let you access or change an element by its index:
int first = nums[0]; // 4 nums[1] = 100; // changes the second element to 100
Valid indices run from
0 to length - 1. Anything outside that range throws an ArrayIndexOutOfBoundsException.int[] nums = {1, 2, 3}; int oops = nums[3]; // ArrayIndexOutOfBoundsException, valid indices are 0, 1, 2
So the first element is always at index 0 and the last one is always at index
length - 1. A negative index throws the same exception.int[] nums = {4, 8, 15, 16, 23}; System.out.println(nums[nums.length - 1]); // 23, the last element System.out.println(nums[-1]); // ArrayIndexOutOfBoundsException
One more thing: an array is an object, so an array variable holds a reference to the array. Copying the variable copies the reference, not the array, so both variables point to the same array:
int[] a = {1, 2, 3}; int[] b = a; // b refers to the same array as a b[0] = 99; System.out.println(a[0]); // 99
This is also why a method that changes the elements of an array parameter changes the original array.