Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

adding map into arraylist has duplicated

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic