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

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




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?

Ask