• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Trying to add Object values to a List, but the last value keeps getting added for all the values

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code:



This sets the projectName to TwoDeviceTest as intended, but I want it to also add these two devices to it. So first I declare the ArrayList to hold the ProjectDevice Objects. Then I set the first ProjectDevice Object's ID to "device, and add the Object to the devices ArrayList. So that ArrayList should now hold one Object, with a value of "device" for its ID. Then I go ahead and do the same thing right after that, and this time set the ID to "device2" and then add that Object to the devices ArrayList. SO that ArrayList should now hold two Objects right? One that has a deviceId of device, and another that has the deviceId of device2.

Then there is more code to add this all to a web service which works fine. But when it adds it I get the following xml:



As you can see there in between the devices resource list, there are two devices as expected, but they both have the same deviceId instead of two different IDs. Why would this happen? Im sure it has to do with my java up there, but it seems to me I create an Object, add it to the ArrayList, then create another Object and add it to the ArrayList, which makes the ArrayList contain two separate values. But instead I end up with two values, but the second one that got added has replaced the first.

Can someone tell me what i did wrong there? Im sure I need to use a for loop or something easy, but I have much more urgent things I need to work on with this project so would like to see what someone says about this easier problem while I mess with the other things.>
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From my reading of the code, you are not adding 2 objects to the ArrayList. You are adding two references to a single object. From the code snippet I can not see how changing the value of the deviceId creates a new ProjectDevice.

Cheers,

John
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the way I saw it, I am first declaring the list t hold the Object:

ArrayList<ProjectDevice> devices = new ArrayList<ProjectDevice>();

And then I set the deviceId variable for that Object:

projectDevice.setDeviceId("device");

Then add that new Object to the List I declared at the top

devices.add(projectDevice);

That should leave me with the devices List populated with one Object. Now I want to populate it with a second Object. So I repeat the process. I first set the deviceId variable in the ProjectDevice Object

projectDevice.setDeviceId("device2");

and then I add this newly formed Object to the List that should already contain the first one I added:

devices.add(projectDevice);

Which seems to me should leave me with a List (devices) that contains two Objects, each with a different value for deviceId.
Then I add this List filled with two Objects to another class

measurementProject.setDevices(devices);

but that doesnt seem to even be relevant


I mean I create the first Object, add it to the device List, and then create another Object and also add it to the device List. SO why wouldnt it have two different Objects instead of two of the same Objects?
 
John Simpson
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the code snippet, you only create one object:


Unless I am missing something, I do not see how changing the deviceId will create a new object of type ProjectDevice.

Adding the object to the ArrayList will add a reference to the object to the list. If you call the ArrayList add method twice with the same object you will end up with two references to the same object. That's my understanding anyway.
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


From the above code, are you creating two objects ?


projectDevice.setDeviceId("device2");
and then I add this newly formed Object to the List that should already contain the first one I added:
devices.add(projectDevice);



How new object will be created if you just change the deviceid of the already existing object "projectDevice"

Hope this helps ,
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what your saying, I thought I was creating the object, and then adding it to the list and that was the end of that. Then I would create yet another object and add it to the list as well on top of the other but apparently it doesnt work that way.

I have too many other huge problems and too little time to worry about this minor issue anyway, thanks for the input though
 
Sheriff
Posts: 22682
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Kohanek wrote:I see what your saying, I thought I was creating the object, and then adding it to the list and that was the end of that. Then I would create yet another object and add it to the list as well on top of the other but apparently it doesnt work that way.


Well, it does work that way. But you do need to create yet another object:
I use the same ProjectDevice reference, but on lines 5 and 9 I am creating two distinct objects of type ProjectDevice. The object I created on line 5 will only have one reference (the ArrayList) after line 9.
 
Matt Kohanek
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i understood what they were telling me and I knew what I needed to do, I was just trying to get a feel of why it was like that. I didn't understand why adding the second Object to the List would also alter the one I had already added, it didn't seem logical to me, but I guess if the program sees it as the same Object it is just trying to help you in changing the values already added to the list that it now sees have been changed.

Thanks for all the help though
 
A feeble attempt to tell you about our stuff that makes us money
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic