<< 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 toArray method

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

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 .

Type is not an issue for the simpler version

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[] .

Size is not an issue for the simpler version

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.

More-complex version presents some issues

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.

The type issue

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.

Two types of objects in this collection

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[] .

Questions & Answers

what is variations in raman spectra for nanomaterials
Jyoti Reply
I only see partial conversation and what's the question here!
Crow Reply
what about nanotechnology for water purification
RAW Reply
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
yes that's correct
I think
what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
analytical skills graphene is prepared to kill any type viruses .
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?