Java Reference
In-Depth Information
Arrays suffer from a significant limitation: their length is fixed. If you start out with
an array of 10 elements and later decide that you need to add additional elements,
then you need to make a new array and copy all values of the existing array into the
new array. We will discuss this process in detail in Section 7.7 .
S YNTAX 7.1: Array Construction
new typeName[length]
Example:
new double[10]
Purpose:
To construct an array with a given number of elements
290
291
S YNTAX 7.2: Array Element Access
arrayReference[index]
Example:
data[2]
Purpose:
To access an element in an array
S ELF C HECK
1. What elements does the data array contain after the following
statements?
double[] data = new double[10];
for (int i = 0; i < data.length; i++) data[i] =
i * i;
2. What do the following program segments print? Or, if there is an error,
describe the error and specify whether it is detected at compile-time or
at run-time.
Search WWH ::




Custom Search