<< Chapter < Page | Chapter >> Page > |
Listing 5 . Copy collection elements into an array. |
---|
array = (JComponent[])ref.toArray(array);showArray(array,"New array contents"); |
The code in Listing 5 also causes the contents of the array to be displayed after it receives the elements from the collection.
The first statement in Listing 5 causes the first seven elements in the array to be overwritten with element values from the collection (plus one null value).
The second statement in Listing 5 causes the contents of the array to be displayed.
The most important thing to note about Listing 5 is that a reference to an array object is passed as a parameter to the toArray method. (The simpler version of the toArray method, discussed in the previous module, doesn'ttake any parameters.)
The essential difference between the two overloaded versions of the toArray method has to do with the origin of the array into which the toArray method copies the elements from the collection.
With the simpler version of the toArray method that takes no parameters. The toArray method creates a new array object of type Object , populates it, and returns that object's reference as type Object .
Since the new array object is of type Object , (when the rules for Generics are not adhered to) there are no issues regarding type compatibility between the type of the array and the types of theelements stored in the collection. A reference to an object of any type can be stored in an array of the generic type Object[] .
Also, since the array is created when it is needed by the simpler version of the toArray method, there are also no size issues. The array is created to be of the correct size to contain copies of all of the elements inthe collection.
With the more-complex version of the toArray method (shown in Listing 5 ) , the programmer must provide the array object that will be populated by the toArray method. In this situation, there are size issues as well as type issues to be dealt with.
Here is some of what the Oracle documentation for the LinkedList class has to say about the type issue for this version of the toArray method:
"Returns an array containing all of the elements in this list in the correct order. The runtime type of the returned array is that of thespecified array. ... Throws: ArrayStoreException - if the runtime type of (the specified array) is not a supertype of the runtime type of everyelement in this list.
In other words, the type of the array passed as a parameter to the toArray method must be a superclass of the classes from which all of the objects beingmanaged by the collection were instantiated.
In this program, the collection is managing objects of the types JButton and JLabel . Each of these types is a subclass of the class named JComponent . For that reason, the type of array that I instantiated and passed to the toArray method is JComponent[] .
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?