• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Whats the easiest way to get certain values out of this list of Objects and add them to a 2nd List

 
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 have

Just a list of Device Objects, each Object contains a deviceId and name variable (both Strings).

I already have the operations performed to load the list with values. I also have this list:

This list of Objects has one variable - String deviceId; I just need to take every deviceId variable from the first list and populate the second list with ProjectDevice Objects, using the deviceIds from the first list as the variable.

Im sure I can do this, but the only ways I can think of doing this would involve 10-20 lines of code I would guess, and I am just wondering if there is an easier way, like some kind of method that already exists to do something like this.

I have heard of a way to cast from one Object to the next, but have never done so and don't really know how, but if that is what I should look into please let me know. Or if there is a simpler way, please let me know.

Thanks


What I am tring to find that seems to be easiest would be the right for statement. In pseudo:

Seems like it could work if my deviceList were a class and not a list of classes. Is there a way to get that to work?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Im sure I can do this, but the only ways I can think of doing this would involve 10-20 lines of code I would guess, and I am just wondering if there is an easier way, like some kind of method that already exists to do something like this



Really? 10 to 20 lines? Just off the top of my head, shouldn't the straightforward solution look something like this? (in pseudo code, of course)



Henry
 
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 I was trying to overshoot that number just in case.

I am not familiar with some of the code concepts you have there such as the first line, here is my own attempt at some pseudo that I edited above while you were replying:


It seems we are actually saying the same thing with our code there now that I look at it. My problem is with the second part of that for loop : deviceList. I guess I should phrase it this way:
How would I make a for loop that says "for every Device in the deviceList (the list full of Device Objects)"...do this

edit 2 - 300th post
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have heard of a way to cast from one Object to the next, but have never done so and don't really know how, but if that is what I should look into please let me know. Or if there is a simpler way, please let me know.




In Java, casting an object from one type to another doesn't change the object. The object should actually also be the target type, and the cast is so that a reference of the target type can reference it. If the object isn't that type, either the compiler will error out, or it will throw a cast exception during runtime (depending on the cast).

So, unless your Device objects are also ProjectDevice objects, you can't cast -- instead, you will have to create new ProjectDevice objects, using data obtained from the Device objects.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am not familiar with some of the code concepts you have there such as the first line



The first line of pseude code is to "create the target array list that will hold the newly created project device objects".

Henry
 
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
No I understood it after I looked more closely. This solution is pretty much what I had in mind when I said I could do this myself in that first post.

What I was really getting at was if there was a way to do this in a single line of code, since each Object has a deviceId variable, and I'm just needing to move that variable into the second list from the first.
Something like this (totally making this up):

devices.cast(deviceList.deviceId);

If you get what Im trying to go for there. But if there isn't a method to do something like this already I can just do this with a for loop

Oh and Device Objects essentially are ProjectDevice Objects, they both represent the exact same things, I just had to create the ProjectDevice Object in order to facilitate some XML stuff. But they both represent the same Objects
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not enough to *represent* the same object "design-wise", they have to be castable: for instance, they could implement the same interface, share a superclass, whatever.

Other options include refactoring the device creation code so the mainline code stays short, or creating a device ctor that takes an object of the other type and does whatever manipulation necessary, or providing a fluent interface, or...
 
rubbery bacon. crispy tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic