This book is now obsolete Please use CSAwesome instead.

8.1. Arrays in Java

The following video is also on YouTube at https://youtu.be/G7aF-OuLfl4. It introduces the concept of an array and gives an example.

An array is consecutive storage for multiple items of the same type. You can store a value in an array using an index (location in the array). You can get a value from an array using an index. An array is like a row of lockers, except that you can’t cram lots of stuff into it. You can only store one value at each array index. An array index is like a locker number. It helps you find a particular place to store your stuff and retrieve stuff.

../_images/rowLockers.jpg

Figure 1: A row of lockers

7-1-2: Can you think of another example of something that is like an array (like a row of lockers)?

Arrays are useful whenever you have several elements of data of the same type that you want to keep track of, but you don’t need to name each one. If you want to keep track of the top 5 highest scores in a game and the names of the people with those scores, you could use two arrays. One array could keep track of the scores and the other the names.

8.1.1. Declaring an Array

To declare an array specify the type of elements that will be stored in the array, then ([ ]) to show that it is an array of that type, then at least one space, and then a name for the array. Note that the declarations below just name the variable and say what type of array it will reference. The declarations do not create the array. Arrays are objects in Java, so any variable that declares an array holds a reference to an object. If the array hasn’t been created yet and you try to print the value of the variable, it will print null (meaning it doesn’t reference any object yet). Try the the following.

8.1.2. Creating an Array

To create an array use the new keyword, followed by a space, then the type, and then in square brackets the size of the array (the number of elements it can hold).

highScores = new int[5];
names = new String[5];

Note

Array elements are initialized to 0 if they are a numeric type (int or double), false if they are of type boolean, or null if they are an object type like String.

../_images/arrayIndicies.png

Figure 2: Two 5 element arrays with their values set to the default values for integer and object arrays.

Note

The first value in an array is stored at index 0 and the index of the last value is the length of the array minus one (since the first index is 0).

8.1.3. Putting Values in an Array

To put a value in an array you give the name of the array and the index number in brackets and then an = and finally the value and a semicolon (highScores[0] = 99;). The first item in an array is at index 0.

8.1.4. Initializing Array Values

You can also initialize (set) the values in the array when you create it. In this case you don’t specify the size of the array, it will be determined from the number of values that you specify.

int[ ] highScores = {99,98,98,88,68};
String[ ] names = {"Jamal", "Emily", "Destiny", "Mateo", "Sofia"};

When you create an array of a primitive type (like int) with initial values specified, space is allocated for the specified number of items of that type and the values in the array are set to the specified values. When you create an array of an object type (like String) with initial values, space is set aside for that number of object references. The objects are created and the object references set so that the objects can be found.

../_images/intAndStringArrays.png

Figure 3: A primitive array and an object array

8.1.5. Array Length

Arrays know their length (how many elements they can store). It is a public read-only field so you can use dot-notation to access the field (arrayName.length). Dot-notation is using variable name followed by a . and then the field (property) name or a method name.

Note

Note that length is a field and not a method, unlike the String length() method, so you don’t add parentheses after length. However, if you use parentheses after length during the exam, you won’t lose any points.

7-1-6: What questions do you have about arrays?

Check your understanding

You have attempted of activities on this page