aspose file tools
The moose likes Java in General and the fly likes Need help with iteration problem (adding an Object to a List for every instance of that Object) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Need help with iteration problem (adding an Object to a List for every instance of that Object)" Watch "Need help with iteration problem (adding an Object to a List for every instance of that Object)" New topic
Author

Need help with iteration problem (adding an Object to a List for every instance of that Object)

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

My end goal is to load a List with every instance of the ProjectHolder Object the following code creates, but I keep ending up with a List that has the last object the following code creates multiple times.

So I have a ProjectHolder class that has two variables -
String projectName;
List<String> devicesList;

Then I have a Nodes class to load this Object with Values. I know this code does load the Object with values, because I have System.out.println() in every for loop


Basically this sets the projectName to the projectName that is stored on a web service, and if there are any devices for this project, it add those device(s) to the deviceList. I am sure that is all working fine, because when I call this loadValues() method, it one by one prints out the projectNames, and if there are any devices, it will print those out as well. Here is a short sample of the print out:

Value of ProjectName PropProp2
Value of ProjectName WSN Pond Measurements
Value of deviceList [WSN-Module-8F3A-D247-4B99]
Value of ProjectName Propertiestest


At the end of the code there you can see I have projects.add(projectHolder); at the end of the for loop. This is the for loop that it is inside of: for (MeasurementProject measurementProject : measurementProjectCollection.getMeasurementProjects())

Its saying for every project in the projectCollection, perform all the operations to set the variables, and then at the end add the newly formed object to the List<ProjectHolder> List;
However what is happening it seems is that it is adding the same Object 33 times, instead of 33 unique Objects. so my projects List<ProjectHolder> contains 33 of the same Object. Can anyone tell me how I can fix this? I need it to add the new object to the List<ProjectHolder> projects; list for every version this method creates.

That is unless I am wrong about my interpretation of this output. I have a print method to print out the value of the projects List. It prints this:

Printing value of projects List [nodesContainer.ProjectHolder@186c6b2, nodesContainer.ProjectHolder@186c6b2, ......etc 31 more time]

Those are all the same Objects in that list right?



True wisdom is in knowing you know nothing - Socrates
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You only create one ProjectHolder instance, outside the for-loop. Move that line to just inside the for-loop instead.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Thanks Rob, that was exactly it. I don't know how long it would have taken me to figure that one little detail out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with iteration problem (adding an Object to a List for every instance of that Object)
 
Similar Threads
issue with object overwriting itself when I need it to create separat instances(nevermind I got it)
Problem getting richfaces tree to display
Looking for correct data type to take in stream of xml
Is a Map data type that holds three objects possible?
need help choosing best tag for this