Wednesday 2 March 2016

Arrays

Next, we turn our attention to the array. We have declared a variable called someCol ors, which is an array of Color objects. In Java, arrays are first-class objects. This means that an array itself is a type of object—one that knows how to hold an indexed list of some other type of object. An array is indexed by integers; when you index an array, the resulting value is an object reference—that is, a reference to the object that is located in the array’s specified slot. Our code uses the colorIndex variable to index someColors. It’s also possible to have an array of simple primitive types, such as floats, rather than objects. When we declare an array, we can initialize it using the curly brace construct. Specifying a comma-separated list of elements inside curly braces is a convenience that instructs the compiler to create an instance of the array with those elements and assign it to our variable. Alternatively, we could have just declared our someColors variable and, later, allocated an array object for it and assigned individual elements to that array’s slots.

0 comments:

Post a Comment