Java Reference
In-Depth Information
The variable height is declared to be an array of integers whose
type is written as int [] . All values stored in an array have the same
type (or are at least compatible). For example, we can create an
array that can hold integers or an array that can hold strings, but
not an array that can hold both integers and strings. An array can be
set up to hold any primitive type or any object (class) type. A value stored in an
array is sometimes called an array element , and the type of values that an array
holds is called the
KEY CONCEPT
In Java, an array is an object that
must be instantiated.
element type of the array.
LISTING 8.1
//********************************************************************
// BasicArray.java Author: Lewis/Loftus
//
// Demonstrates basic array declaration and use.
//********************************************************************
public class BasicArray
{
//-----------------------------------------------------------------
// Creates an array, fills it with various integer values,
// modifies one value, then prints them out.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final int LIMIT = 15, MULTIPLE = 10;
int [] list = new int [LIMIT];
// Initialize the array values
for ( int index = 0; index < LIMIT; index++)
list[index] = index * MULTIPLE;
list[5] = 999; // change one array value
// Print the array values
for ( int value : list)
System.out.print (value + " ");
}
}
OUTPUT
0 10 20 30 40 999 60 70 80 90 100 110 120 130 140
 
Search WWH ::




Custom Search