Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

An object array is a fixed-size sequence of objects:

The subscript used in constructing object arrays is evaluated as an integer:

We can specify the initial collection of contained objects when we construct the array. Those objects can be any other entity in Groovy, eg, numbers, boolean values, characters, strings, regexes, lists, maps, closures, expandos, classes, class instances, or even other object arrays:

We can make a shallow copy using clone():

We have a special syntax for constructing multi-dimensional object arrays with null initial values:

We must specify the size of at least the first, outermost, dimension of an object array when we first create it:

A multidimensional array need not have arrays of the same length at each level. Thus, a triangular matrix may be created by:

There are strict rules concerning evaluation when subscripting object arrays:

Implementing an ArrayList with an Object Array

ArrayLists are implemented with object arrays internally. Each ArrayList instance has a capacity, the size of a fixed-size array used to store the elements. This array is always at least as large as the list size, and its capacity grows automatically as elements are added to the list. To see the internal capacity of lists constructed with various values:

For efficiency, we can increase the capacity of a list before adding a large number of elements:

We can see how many times a list has been modified:

  • No labels