Java Reference
In-Depth Information
317
A Partially Filled Array
Now data.length is the capacity of the array data, and dataSize is the
current size of the array (see A Partially Filled Array). Keep adding elements into
the array, incrementing the dataSize variable each time.
data[dataSize] = x;
dataSize++;
This way, dataSize always contains the correct element count. When you run
out of space, make a new array and copy the elements into it, as described in the
preceding section.
Array lists use this technique behind the scenes. An array list contains an array of
objects. When the array runs out of space, the array list allocates a larger array and
copies the data. However, all of this happens inside the array list methods, so you
never need to think about it.
A DVANCED T OPIC 7.5: Methods with a Variable
Number of Parameters
Starting with Java version 5.0, it is possible to declare methods that receive a
variable number of parameters. For example, we can modify the add method of the
DataSet class of Chapter 6 so that one can add any number of values:
data.add(1, 3, 7);
Search WWH ::




Custom Search