<< Chapter < Page Chapter >> Page >

Copy collection elements into an array

The code in Listing 6 shows how to use the simple version of the toArray method to create an array of type Object that contains a copy of each element in the LinkedList collection.

Listing 6 . Copy collection elements into an array.
Object[] array = ref.toArray();

The toArray method

Here is some of what Oracle has to say about this version of the toArray method:

"Returns an array containing all of the elements in this collection. If the collection makes any guarantees as to what order its elements arereturned by its iterator, this method must return the elements in the same order.
The returned array will be "safe" in that no references to it are maintained by this collection. ... The caller is thus free to modify thereturned array."

I will have some more to say about the safe aspects of the array shortly.

Display the array contents

The code in Listing 7 calls a method named showArray to cause the current contents of the array to be displayed.

Listing 7 . Display the array contents.
System.out.println("New array contents"); showArray(array);

The entire showArray method is shown in Listing 8 .

Listing 8 . The showArray method.
void showArray(Object[] array){for(int i = 0; i<array.length;i++){ if(array[i]== null){ System.out.print("null ");}else{ System.out.print(((JComponent)array[i]). getToolTipText() + " ");}//end else }//end for loopSystem.out.println(); }//end showArray()

The showArray method

The behavior of the ShowArray method is straightforward. The method uses a for loop to access each of the elements stored in the array in increasing index order.

A test is made to determine if the element contains a null reference. If so, then the word null is displayed for that element. If not, the getToolTipText method is used to access and display the value of the toolTipText property for each element in the array.

The output for the array

The output produced by the code in Listing 8 is shown below:

New array contents B0 B1 L2 B3 B4 L5

As you can see, (except for the String that identifies the type of output) this is an exact match to the output produced when the contents of thecollection were displayed.

How "safe" is the array?

While it is "safe" to modify the contents of the array as explained in the quotation from Oracle earlier, there is still some danger here that you need tobe aware of.

Java collections do not store objects. Rather, Java collections store references to objects. In Java, it is entirely possible to have two or morereferences to the same object.

Array contains copies of references to objects

Each element in the array is a copy of an element in the collection.

Therefore, at this point, for each object being managed by the collection, at least two references exist that refer to that object. One copy is contained inthe collection. The other copy is contained in the array.

If you use a reference stored in the array to modify the state of one of those objects, that modification is made to the object that is also referencedby an element in the collection. This may or may not be what you intend. It's not necessarily a problem as long as you understand what is going on and becareful how you use the references stored in the array.

Questions & Answers

what are the products of Nano chemistry?
Maira Reply
There are lots of products of nano chemistry... Like nano coatings.....carbon fiber.. And lots of others..
Even nanotechnology is pretty much all about chemistry... Its the chemistry on quantum or atomic level
Preparation and Applications of Nanomaterial for Drug Delivery
Hafiz Reply
Application of nanotechnology in medicine
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 .
Any one who tell me about Preparation and application of Nanomaterial for drug Delivery
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
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?