• Post Reply Bookmark Topic Watch Topic
  • New Topic

array to arraylist  RSS feed

 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily write a little program to test this.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried the equals() method and it gives false. but i want to confirm this
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Saloon Keeper
Posts: 7928
143
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh thats a pretty good explanation and quite convincing !           

thanks  !!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!