Chapter 7 Arrays and References
Up to this point, the only variables we have used were for individual values such as numbers or strings. In this chapter, you’ll learn how to store multiple values of the same type by using a single variable. This language feature will enable you to write programs that manipulate larger amounts of data.
For example, Exercise 5 asked you to check whether every letter in a string appears exactly twice. One algorithm (which hopefully you already discovered) loops through the string 26 times, once for each lowercase letter:
| // outer loop: for each lowercase letter for (char c = 'a'; c <= 'z'; c++) { // inner loop: count how many times the letter appears for (int i = 0; i < str.length(); i++) { ... // if the count is not 0 or 2, return false |
This “nested loops” approach is inefficient, especially when the string is long. For example, there are more than 3 million characters in War and Peace; to process the whole book, the nested loop would run about 80 million times.
Another algorithm would initialize 26 variables to zero, loop through the string one time, and use a giant if statement to update the variable for each letter.
But who wants to declare 26 variables?
That’s where arrays come in.
We can use a single variable to store 26 integers.
Rather than use an if statement to update each value, we can use arithmetic to update the nth value directly.
We will present this algorithm at the end of the chapter.
7.1 Creating Arrays
An array is a sequence of values; the values in the array are called elements.
You can make an array of ints, doubles, Strings, or any other type, but all the values in an array must have the same type.
To create an array, you have to declare a variable with an array type and then create the array itself.
Array types look like other Java types, except they are followed by square brackets ([]).
For example, the following lines declare that counts is an “integer array” and values is a “double array”:
| int[] counts; double[] values; |
To create the array itself, you have to use the new operator, which you first saw in Section 3.2.
The new operator allocates memory for the array and automatically initializes all of its elements to zero:
| counts = new int[4]; values = new double[size]; |
The first assignment makes counts refer to an array of four integers.
The second makes values refer to an array of doubles, but the number of elements depends on the value of size (at the time the array is created).
Of course, you can also declare the variable and create the array with a single line of code:
| int[] counts = new int[4]; double[] values = new double[size]; |
You can use any integer expression for the size of an array, as long as the value is nonnegative.
If you try to create an array with -4 elements, for example, you will get a NegativeArraySizeException.
An array with zero elements is allowed, and there are special uses for such arrays.
You can initialize an array with a comma-separated sequence of elements enclosed in braces, like this:
| int[] a = {1, 2, 3, 4}; |
This statement creates an array variable, a, and makes it refer to an array with four elements.
7.2 Accessing Elements