Forums Register Login

array to arraylist

+Pie Number of slices to send: Send
when the asList() method is used, the List reference that is returned is backed by the array right.
what does this exactly mean?


1. are there 2 data structures in the memory, i.e. the original array  instance and the newly generated list instance , that are both connected so that changes in one are reflected in the other.


OR

2. is it only one data structure in the memory that can be accessed and manipulated  by both the original array reference and new list reference ?
+Pie Number of slices to send: Send
You can easily write a little program to test this.
+Pie Number of slices to send: Send
i don't think i will get the answer by that . because the result is that both affect each other. but i don't know exactly what is happening in the memory , if its two separate data stores or one data store .
+Pie Number of slices to send: Send
i tried the equals() method and it gives false. but i want to confirm this
1
+Pie Number of slices to send: Send
Ahh I see what you mean. Anyway, the phrase "backed by" means that the List doesn't have its own new data store, but actually uses the array you passed to the asList() method.

In general, methods that look like to...() return objects that live separate lives from the object you created them from, while methods that look like as...() return objects that share the same backing data structure as the object they were created from.
+Pie Number of slices to send: Send
equals() won't help you. The List you get from asList() is still a different object (with a different type!) than the array you created it from. However, internally it uses the same array, so if you edit the array the list will appear to change as well.
+Pie Number of slices to send: Send
okay i get it that they both refer to the same location in the memory ! but i don't understand why the equals() method returns a false. if internally they mean the same memory, then why equals gives false ? thats what the equals() actually does right , it checks for the reference equality except for classes in which it has been custom implemented such as String, Arraylist, etc.
(1 cow) 3
+Pie Number of slices to send: Send
No, they don't refer to the same location in memory. asList() returns a new List object, that internally uses the existing array, instead of a new array.

Here's roughly what happens:

As you can see, this returns a new List, which is a different object from the array. The methods of the list don't use an internal storage, but instead they use the array that was passed to the asList() method.
+Pie Number of slices to send: Send
oh thats a pretty good explanation and quite convincing !            

thanks  !!!
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 407 times.
Similar Threads
Can't get array-backed List to "write through" back to the array
NX: URLYBird / my approach of the reading problem
Collection
Proper Practice when checking a List in a Constructor
Garbage Collection Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:16:22.