<< Chapter < Page | Chapter >> Page > |
As an aside, let me mention that there is a class named Array , which provides static methods to dynamically create and access Java arrays. The use of themethods of this class makes it possible to handle arrays with a programming style similar to the programming style typicallyused with ordinary objects. However, the use of the methods of the Array class tends to require more programming effort than the square-bracket notation discussed in this module. Iwill discuss a sample program that illustrates the methods of the Array class in a future module.
Encapsulating a group of variables
As is the case with other languages that support arrays, array objects in Java encapsulate a group of variables.
Zero or more variables may be encapsulated in an array object. If the number is zero, the array object is said to be empty.
(An example of an empty array object is the String[] array passed to the main method in a Java application when the user doesn't enter any arguments at the command line.)
No individual names
Also, as with other languages that support arrays, the variables encapsulated in an array object don't have individual names. Rather, they are referencedusing positive integer index values.
(Typically, in Java, the index is placed in square brackets, which are applied to the name of the reference variable holding a reference to the arrayobject.)
Elements or components?
It is common in the literature to refer to the variables that make up an array as its elements . However, the Java specification refers to them as components. The specification ascribes a different meaning to the word element, as shown in the following quotation from the specification:
"The value of an array component of type float is always an element of the float value set ...; similarly, the value of an array component of type double is always an element of the double value set."
Another quotation from Sun (shown later in this module) provides a somewhat clearer distinction between the words component and element .
(However, from force of habit, I will probability use component and element interchangeably in this module.)
The length of an array
If an array has n components, the length of the array is n . The components of the array are referenced using integer indices from 0 to (n -1), inclusive.
Another quotation from Sun
Here is another quotation from the Java specification that explains the type specifications for the variable declarations in Listing 1 and Listing 3 .
"All the components of an array have the same type, called the component type of the array. If the component type of an array is T, then the type of thearray itself is written T[]."
Components may be of an array type
As of this writing, all array objects in Java encapsulate one-dimensional arrays (I have read that this may change in the future).
The component type of an array may itself be an array type. This makes it possible to create array objects whose individual components refer to otherarray objects.
Multi-dimensional or ragged arrays
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?