End of the Chapter
Here is a list of facts about arrays.
You may wish to refer back to a page that discusses a particular fact
in greater detail.
-
An array
is an object that has room for several values, all of the same type.
- Each value is stored in a slot of the array.
- If there are N slots in the array, the slots are indexed from 0 up to (N-1).
- The index must be an integer value (byte, short, int, or long.)
-
An array declaration
looks like:
int[] intArray;
This declaration only declares the array reference intArray.
It does not create the actual object.
- An array can be declared and constructed in a combined statement:
int[] intArray = new int[17];
This declaration declares the array reference intArray,
and constructs an array object containing 17 slots that can hold int.
- When an array object is constructed using the new operator,
the slots are
initialized
to the default value of the type of the slot.
Numeric types are initialized to zero.
- Once an array object has been constructed, the number of slots it has
can not be changed. (However, a completely new array object,
with a different number of slots, can be
constructed to replace the first array object.)
-
An indexed array name,
such as intArray[12] can be used anywhere
an ordinary variable of the same type can be used.
- The index used with an array can be stored in a variable, for example
int j = 5 ;
intArray[ j ] = 24; // same as: intArray[ 5 ] = 24
- The index used with an array can be computed in an expression, for example
int j = 5 ;
intArray[ j*2 + 3 ] = 24; // same as: intArray[ 13 ] = 24
-
The index
used with an array must be within the range 0..(N-1) where N is
is number of slots of the array.
- If an index that is out of bounds is used with an array, an exception is
thrown and the program stops running (unless it catches the exception.)
- An array can be declared, constructed, and initialized using an
initializer list.
This can only be done when the
array is first declared.
The next chapter will
discuss further aspects of arrays.
Click here to go back to the main menu.
|