Clusters of Data


An array is a cluster of variables, called components, all of the same type. The array has a name, but the individual variables within the array do not. Each of the components has a unique identifying integer, called an index. The plural of index is indices, which proves that someone was paying attention in Latin class. The indices range from 0 through n-1, where n is the number of components in the array.

Before you use an array, you have to declare its type and create it. You have already seen type declarations in the context of primitive data types, and array declaration is quite similar. Creation is a new concept, and we will discuss it in some depth.

Declaring Arrays

Array declaration, like primitive declaration, associates a variable name with a data type. There are two ways to declare an array. The preferable format is

 Component_type[] name; 

Note the square brackets after the component type. An example of this format is

float[] temperaturesCelsius;

This declaration says that temperaturesCelsius is the name of an array whose components are all of type float. The number of components will be specified later, when the array is created. Note the plural in the name, indicating that the variable relates to more than one temperature.

The alternative format for array declaration is

 Component_type name[]; 

The only difference is that the empty square brackets now come after the name, rather than after the component type. This format is included as a holdover from older programming languages (C and C++). It is considered less readable than the first format, and we will not use it in this book.

Creating Arrays

At some point after you declare an array, you need to create it. This is new. With primitives, all you had to do was declare a variable's type, and the variable came into existence. Arrays, as well as all other kinds of objects, are different: You have to create them explicitly. This is done with the keyword new.

The format of an array creation statement is

 name = new component_type[number_of_components]; 

In the previous section, you declared a variable named temperaturesCelsius to be an array whose components have float type. From this point on, we will say this more briefly: temperaturesCelsius is an array of floats. When you want to create the array, you first have to decide how many components it will have. If you want 10 components, for example, you would create the array like this:

temperaturesCelsius = new float[10];

The array size does not have to be a literal int. A variable is acceptable. Suppose you have a method called getNTempReadings, which returns the number of temperatures available to the program. Then you might do the following:

int nTemps = getNTempReadings(); temperaturesCelsius = new float[nTemps];

You could even get more terse:

temperaturesCelsius = new float[getNTempReadings()];

When an array of primitives is created, all of its components are given an initial value. Numeric arrays (that is, arrays of byte, short, int, long, float, and double) have all components initialized to 0. Boolean arrays have all components initialized to false. Char arrays have all components initialized to the null character, which is a non-printing, do-nothing zero value that indicates "no character at all."

It is convenient to represent an array as shown in Figure 6.1.


Figure 6.1: A new array

Figure 6.1 shows the array after it has been created but before any of its components have been modified. Now is the perfect time to learn how to modify the components.

Using Array Components

The components of an array of n components have indices from 0 through n-1. The names of those components are arrayName[0], arrayName[1], and so on, through arrayName[n-1]. Thus, in this example, you could use the following code to set the first and last components to -10 and 10, respectively:

temperaturesCelsius[0] = -10; temperaturesCelsius[9] = 10;

Sometimes the first component is called the zeroth component, so that the adjective will match the index. The term eliminates confusion, because one could reasonably (but wrongly) believe that the first component is temperaturesCelsius[1].

Figure 6.2 shows the array after the preceding code has been executed.


Figure 6.2: A used array

Of course, you can also read the value of an array component. The following code sets a component to the average of two other components:

temperaturesCelsius[5] =   (temperaturesCelsius[6]+ temperaturesCelsius[7]) / 2;

You can see that an individual array component can be used in any context where a primitive variable of the same type can be used.

Array Length

When you create an array, you specify the number of components. Subsequently, the array knows its component count. You can read the number of components with the expression arrayName.length. For example, if you have an array called employeeNames, the following code sets an int called nEmployees to be the length of the array:

nEmployees = employeeNames.length;

The array's length is permanently fixed at creation time, so you can't modify it. The following code would cause a compilation error:

employeeNames.length = 5000;

Array Initialization

We have already said that when an array is created, its components are initialized to zero for numeric types, or to false or the null character for booleans and chars. If you want the array to start life with different contents, you can set the values of the components you want to change one by one, such as follows:

char[] chars = new char[10]; chars[3] = 'L'; chars[4] = 'C';

If you want to specify a value for all the components of the new array, a more compact syntax is available:

name = new type[] {value0, value1, … };

The compiler determines the array length by counting the values in the curly brackets. The array components are initialized to those values. For example, the following code creates and initializes an array of 4 bytes:

byte[] bytes = new byte[] { 3, 5, 7, 99};




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net