Forums Register Login

toArray() in arraylist

+Pie Number of slices to send: Send
can you please explain this concept
If the returned array of toArray() is modified ,say swapping the position of its elements or by assigning new objects to its elements ,the elements of arraylist wont be effected.but if you modify the state of elements of the returned array,then the modified state of elements will be reflected in the arraylist. what does state here refer to?? and also please explain th whole concept in the paragraph
+Pie Number of slices to send: Send
Can you please tell from where you are quoting the above snippet?
It would be better to understand the context first so that we perceive in the same way author is quoting it.
+Pie Number of slices to send: Send
This thread will answer all your questions.
+Pie Number of slices to send: Send
 

meghana chintanippu wrote:can you please explain this concept
If the returned array of toArray() is modified ,say swapping the position of its elements or by assigning new objects to its elements ,the elements of arraylist wont be effected.but if you modify the state of elements of the returned array,then the modified state of elements will be reflected in the arraylist. what does state here refer to?? and also please explain th whole concept in the paragraph



Actually it's just two wrappers holding the same objects now. If you remove an object from one wrapper ( either the ArrayList or the array ) , the object will still be present in the other wrapper. Why should there be a change? If you arrange the objects in the array in one way, why should the ArrayList be affected? It has its own order in which those objects are placed and you haven't changed that order.

If you change the state of an object, obviously it will affect both wrappers ( the ArrayList and the array ), cause both the wrappers have the same object. You are here making changes to the actual object state. So whoever is pointing to this object will be able to see the change.

It is the same object the array is holding.

what does state here refer to??


State refers to exactly what it means in English. The state of the object at a given time is determined by the value(s) at a given time of what the object has( English).

For example, when you create an Employee object-

Lets say you have your Employee class as



and you create an Employee object such as


this is one state the object obj is at a given time till someone changes it.

If someone does this--


The object obj is said to be in a different state now. This is also referred to as 'mutation' or 'state change'.

HIH.

Edit : It's also worth mentioning that when you create an array using the toArray method, the resultant array actually holds new references to the same objects. This is in contrast with the asList() method where the resultant List doesn't really get other references created for the same object. The resultant List in the latter case is just a snapshot of the source array.
+Pie Number of slices to send: Send
ok thanka for the explanation....
+Pie Number of slices to send: Send
 

Chan Ag wrote:This is in contrast with the asList() method where the resultant List doesn't really get other references created for the same object. The resultant List in the latter case is just a snapshot of the source array.



This is sort of misleading. Actually the resultant List you get after invoking asList() on an array, is just a view. It contains no data, i.e no objects.
So many edits, anyway ..

meghana chintanippu wrote:ok thanka for the explanation....



I shall mark the topic as resolved.



+Pie Number of slices to send: Send
It is from mala gupta book for ocap java7 certification
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 557 times.
Similar Threads
Doubt in ArrayList's toArray function
Collection.toArray() method
Problem in passing ArrayList
Page 579 of SCJP 6 book
Question on ArrayList element reference when toArray() called
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:04:22.