wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes problem with arraylist element copying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with arraylist element copying" Watch "problem with arraylist element copying" New topic
Author

problem with arraylist element copying

Wasif Kirmani
Greenhorn

Joined: Sep 19, 2008
Posts: 3
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with arraylist element copying
 
Similar Threads
Sorting 10 numbers
Accessing elements from an arraylist
How does enhanced for loop behave?
JSTL forEach syntax
JPQL: iterator on several entities