• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with arraylist element copying

 
Wasif Kirmani
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem with copying element from one list to another list
//Let assume groupName in groupUserEntity \is "ABC" in first iteration and "DEF" in second iteration and "EFG" in third iteration whenever it save goes in 2nd iteration it change the first added element to the second one and then in the third iteration it converts both to the value in third iteration.... why this problem is coming... also explain me the problem if you can...

Thanks in Advance
[edit] Move description of problem out of code tags. CR[/edit]
[ December 08, 2008: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 48938
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't use abbreviations like --u; write "you;" see this FAQ. I had to correct that spelling error before I could edit your post.
And please don't put ordinary text in the CODE tags; it is very difficult to read; I moved it to make it easier to read.
I don't understand the problem, I am afraid. I thought at first it was to do with the .setGroupName(tempGroupName) call, but that appears unlikely.

Try putting some debugging lines in, and print the status of the different variables as you go through that loop. Sorry I can't help any more at present.

Anybody else?
[ December 08, 2008: Message edited by: Campbell Ritchie ]
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let assume groupName in groupUserEntity \is "ABC" in first iteration and "DEF" in second iteration and "EFG" in third iteration whenever it save goes in 2nd iteration it change the first added element to the second one and then in the third iteration it converts both to the value in third iteration.... why this problem is coming... also explain me the problem if you can...


Well, the example does really do a good job at showing this, but I am willing to bet that you have a mutable object being loaded into a collection, like so (psuedo code)...



Keep in mind that a collection hold references. So, in this example, you are adding the same group name reference many times, and changing the name as you go. You'll wind up with multiple copies of the same group name, with the group name set to the name of the last iteration.

Henry
[ December 08, 2008: Message edited by: Henry Wong ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic