8.1 Tables and Arrays

The table shown below gives the average Canadian retail price for one litre of regular gasoline for the period from 1980 to 2000.

	

If we wanted to keep track of these data in a computer program, we could use five variable identifiers: price1980, price1985, price1990, price1995, and price2000. However, this is a bit cumbersome and, if we wanted to extend our data to keep a record of the price every year or even every month, then this solution would become totally unwieldy.

To overcome this problem, Java offers a very useful data structure, the array - a collection of data items of the same type. An array to hold our gasoline price data would require five elements. Java stores the elements of an array in consecutive locations in memory. An array has a single identifier with the elements being distinguished by an index. In Java, the values of the indices (plural of index) always start at zero. If we were to store our five gasoline prices in an array called price, then the values of the indices would range from zero to four. We can picture the array as shown in the following diagram. The identifiers of the individual elements of the array are price[0], price[1], ... , price[4].

	

In using arrays, it is important to distinguish between the elements of the array and the indices. In the price array, the elements are the different prices of gasoline while the indices mark the locations in the array of the elements. The elements here are floating point values while the indices are integers. It is also important to distinguish between the identifiers of the elements and the values of the elements. For the price array, the identifiers are price[0] and so on while the values are 0.27 and so on.

Example 1

The temperatures of a solution over a six-minute interval are recorded in the following table:
    
An array temp could be used to record the temperatures of a solution each minute as follows:
	
The identifier of the array is temp.
The identifiers of the elements are temp[0] , temp[1], ... , temp[6].
The values of the elements are 32.0, 27.4, ... , 20.6
The indices are 0, 1, ... , 6.

In Java, an array is a kind of object. To create an array, we proceed in a way very similar to that used in creating any object. For example, to create an array to hold the gasoline price data, we could begin with the declaration

	double[] price;

This creates the variable price and determines that the type of price is a reference to an array of double values. As with other objects, the declaration, by itself, does not actually create an array. To get Java to allocate memory for an array of five double values, we can now write

	price = new double[5];

Pictorially, the result of this statement is:

	

Notice the value 0.0 shown in each element of the array. Arrays are automatically initialized when we allocate space to them. If the elements are numeric, they are initialized to zero (of the appropriate type); if they are char values, they are initialized to the null character with representation consisting of 16 zero bits; if they are boolean they are initialized to false; if they are references, they are initialized to null.

Again, as with any object, we can combine the declaration of the array reference variable and the allocation of memory into one statement. For the price array, we could have written

	double[] price = new double[5];

to declare, allocate memory for, and initialize the price array in one statement.

This can be done in general. A statement of the form

<type>[] <identifier> = new <type>[<expression>];

declares that <identifier> is an array whose components are of type <type> and also allocates space in memory for such an array.(Note: Java also allows us to write array declarations in the form <type> <identifier> [] with the square brackets following the identifier but we will not use that form.) The size of the array is determined by the value of <expression>. This is often simply a positive integer constant but it can be any expression that has a positive value and is of any integer type other than long.

Example 2

The following fragment declares an int array list of size 20.
   byte b = 10;
   int[] list = new int[2*b];

Once an array has been declared, its size is fixed for the duration of its existence. Every array object has a length field whose value can be obtained by appending .length to the identifier of the array. The value of an expression of the form <identifier>.length is the number of elements in the array. Since arrays are always indexed from zero, the length of an array is one greater than the highest index in the array.

Java has an alternative form of array declaration that allows us to initialize an array with any values that we like. The next example illustrates this feature.

Example 3

The following statement declares an int array called primes and initializes it with the values of the first ten prime numbers.
   int[] primes = {2,3,5,7,11,13,17,19,23,29};

Notice in the example the absence of the keyword new and the use of brace brackets rather than square brackets on the right side of the assignment operator. In addition, the size of the array is not stated explicitly; Java knows that the array is of length ten because there are ten values inside the brace brackets. The values inside the brace brackets need not be constants; they can be expressions which will be evaluated at the time that the program is executed. The only restriction on these expressions is that they all be of the same type since (as we said at the beginning of this section) arrays are collections of values of the same type.

Although Java's arrays are always indexed from zero, sometimes the values that we are dealing with are more naturally indexed from some other value. Suppose, for example, that we wanted to store the values of a sequence that has terms t1, t2,...,tn. We could use an array of length n but then ti would be stored in the location with index i-1. An alternative (that we favour) is to declare the array to be of size n + 1 and then store ti at location i with the location having index zero being unused.

Example 4

Suppose that we wanted to store the sequence with terms
 

in an array of double values. We could create and initialize such an array by writing

 double[] terms = {0.0,0.5,0.25,0.125,0.0625, 0.03125};
This would store ti at the location with index i. The location with index zero would be unused.

Exercises 8.1

  1. An array to store marks for twenty students has been declared as follows:
     int[] marks = new int[20];
    (a) What is the array identifier?
    (b) What is the identifier of the first element in the array?
    (c) What value is stored in each element by the declaration?
    (d) What is the value of marks.length?
    (e) What are the indices of the array?

  2. How much space (in bits) would be required to store the elements of each array?
    (a) int[] a = new int[20];
    (b) double[] b = new double[100];
    (c) float [] c = new float[50];
    (d) boolean[] d = new boolean[1000];

  3. Write declarations to create arrays that would be appropriate for storing the indicated data.
    (a) the numbers of votes cast for five candidates in an election
    (b) the answers to a twenty-question true/false quiz
    (c) average family size in the years 1900, 1910, ... , 2000

  4. (a) Write a statement that creates and initializes an array terms of double values to store the terms of the sequence
    	
    (b) What is the value of terms.length?

  5. The table gives atomic masses of the eight lightest elements listed according to atomic number.
    	
    Suppose that the data in this table were to be represented by the array mass declared by the statement
    	double[] mass = {0,1,4,6.9,9,10.8,12,14,16};
    (a) What is the value of mass[2]?
    (b) What is the value of mass[5]?
    (c) What are the possible values of the indices of the array?
    (d) What is the identifier of the element whose value is 6.9?
    (e) Of what type are the elements?
    (f) What is the value of mass.length?