• Post Reply Bookmark Topic Watch Topic
  • New Topic

adding map into arraylist has duplicated  RSS feed

 
feras almasri
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I have a map list updated during condition procedure and at the end of the procedure I add the map to the arraylist before the second update.
when I print the map before I add it to the arraylist it shows me correct answer but when I print the arraylist at the end it gives me the same result for all the maps inside it.

it seems like it copying the last map into multiple items instead of adding a different map in the arraylist each time I use add.



The code up shows that when I print the map it shows me different answer each time I call it.



put at then end the all result of this arrylist is the same
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will have to show us the code you use to add to the list, and the code you use to make new maps.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like Steve says we need to see code but it's probably because you aren't creating a new instance of the map during your "condition procedure" and so you are adding the same map instance to the list multiple times. Also if you are using the same keys each time through the "condition procedure" the map will entries will be overwritten each time and so each list entry will show as being the last map returned by your "condition procedure".
 
feras almasri
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The mistake was that I didn't use new map each time I want to add a new map in the list. but I can't understand why it needs that?

Thanks
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
feras almasri wrote:The mistake was that I didn't use new map each time I want to add a new map in the list. but I can't understand why it needs that?

Thanks

Because you're not actually adding the map to the list, you are adding a reference to the map. So if you added the 'map' to the list several times, all you actually have is a list of references to the same map. If you then change the map, all those references will be pointing to the changed map.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!