3. Arrays in Java¶
3.1. Objectives¶
- Declare and initialize an array
- Use an index to set and retreive values from an array
3.2. Time Goal¶
- 20 minutes on this section
3.3. Key Terms¶
3.4. Exercises¶
Note
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.
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.
Note
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. 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 an array yet). Try the the following.
Note
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
.
Note
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.
Note
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"};
Note
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 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.
3 | 2 | 1 | -3 |
-
Q-230: What index is the first element in an array at?
- 0
- The index is really telling the computer how far the item is from the front of the array. So the first element in an array is at index 0.
- 1
- While this matches with how we number some things, the first item in an array is at index 0.
4 | -2 | 8 | 7 |
-
Q-231: Which index is the last element in an array called
highScores.length
- Look at the example above when we were setting the values for the highScore array.
highScores.length - 1
- Since the first element in an array is at index 0 the last element is the length minus 1.
highScores
at?
-
Q-232: Which of the following declarations will cause a compile time error?
int[] scores = null;
- You can initialize an array reference to null to show that it doesn't refer to any array yet.
int[] scoreArray = {50,90,85};
- You can provide the values for an array when you declare it.
String[] nameArray = new String[10];
- You can declare and array and create the array using the
new
operator in the same statement. String[] nameArray = {5, 3, 2};
- You can not put integers into an array of String objects.
int[] scores = new int[5];
- You can declare and array and create it in the same statement. Use the
new
operator to create the array and specify the size in square brackets.
-
Q-233: What is returned from
- 1
- This would be returned from
arr[2]
. - 2
- This returns the value in
arr
at index 3. Remember that the first item in an array is at index 0. - 3
- This would be returned from
arr[1]
. - 6
- This would be returned from
arr[0]
. - 4
- This would be returned from
arr.length
arr[3]
if arr={6, 3, 1, 2}
?