Using Arrays in Java: An Example
Here is a programming example showing how arrays are used in Java. The Java Motel, with its ten comfortable rooms, sits in a quiet place off the main highway. Aside from a small, separate office, the motel is just one long row of ground floor rooms. Each room is easily accessible from the spacious front parking lot.
Oddly enough, the motel’s rooms are numbered 0 through 9. In this example, you’re trying to keep track of the number of guests in each room. Because you have ten rooms, you may think about declaring ten variables:
int guestsInRoomNum0, guestsInRoomNum1, guestsInRoomNum2, guestsInRoomNum3, guestsInRoomNum4, guestsInRoomNum5, guestsInRoomNum6, guestsInRoomNum7, guestsInRoomNum8, guestsInRoomNum9;
Doing it this way may seem a bit inefficient — but inefficiency isn’t the only thing wrong with this code. Even more problematic is the fact that you can’t loop through these variables. To read a value for each variable, you have to copy the nextInt method ten times.
guestsInRoomNum0 = diskScanner.nextInt(); guestsInRoomNum1 = diskScanner.nextInt(); guestsInRoomNum2 = diskScanner.nextInt();
… and so on.
Surely a better way exists.
That better way involves an array. An array is a row of values, like the row of rooms in a one-floor motel. To picture the array, just picture the Java Motel:
First, picture the rooms, lined up next to one another.
Next, picture the same rooms with their front walls missing. Inside each room you can see a certain number of guests.
If you can, forget that the two guests in Room 9 are putting piles of bills into a big briefcase. Ignore the fact that the guests in Room 6 haven’t moved away from the TV set in a day and a half. Instead of all these details, just see numbers. In each room, see a number representing the count of guests in that room. (If freeform visualization isn’t your strong point, look at this figure.)An abstract snapshot of rooms in the Java Motel.
In the lingo here, the entire row of rooms is called an array. Each room in the array is called a component of the array (also known as an array element). Each component has two numbers associated with it:
The room number (a number from 0 to 9), which is called an index of the array
A number of guests, which is a value stored in a component of the array
Using an array saves you from all the repetitive nonsense in the sample code shown at the beginning of this section. For instance, to declare an array with ten values in it, you can write one fairly short statement:
int guests = new int;
If you’re especially verbose, you can expand this statement so that it becomes two separate statements:
int guests; guests = new int;
In either of these code snippets, notice the use of the number 10. This number tells the computer to make the guests array have ten components. Each component of the array has a name of its own. The starting component is named guests, the next is named guests, and so on. The last of the ten components is named guests.
In creating an array, you always specify the number of components. The array’s indices start with 0 and end with the number that’s one less than the total number of components.
The snippets give you two ways to create an array. The first way uses one line. The second way uses two lines. If you take the single line route, you can put that line inside or outside a method. The choice is yours. On the other hand, if you use two separate lines, the second line, guests = new int, should be inside a method.
In an array declaration, you can put the square brackets before or after the variable name. In other words, you can write int guests or int guests. The computer creates the same guests variable no matter which form you use.