How to: create a list of objects?

Discussion in 'Java' started by Ook, Jan 17, 2007.

  1. Ook

    Ook Guest

    Pardon the n00b question - How would you go about creating a list of
    objects, where each object can contain a list of objects, and each
    sub-object would have various properties(string, int)?

    This is an example. The parent object would be forms of transportation -
    car, bike, etc. Each parent object has child objects (chevy, ford, etc.) and
    each of these child objects has the same properties (number of doors,
    color). How would I represent this data? I need some pointers to start me in
    the right direction.

    Car - Chevy - 4 doors, green paint
    - Ford - 2 doors, purple paint
    - Nissan - 3 doors, red paint
    Bike - 26 inch, green
    - 27 inch, purple
    Ook, Jan 17, 2007
    #1
    1. Advertising

  2. Ook

    Daniel Pitts Guest

    Ook wrote:
    > Pardon the n00b question - How would you go about creating a list of
    > objects, where each object can contain a list of objects, and each
    > sub-object would have various properties(string, int)?
    >
    > This is an example. The parent object would be forms of transportation -
    > car, bike, etc. Each parent object has child objects (chevy, ford, etc.) and
    > each of these child objects has the same properties (number of doors,
    > color). How would I represent this data? I need some pointers to start me in
    > the right direction.
    >
    > Car - Chevy - 4 doors, green paint
    > - Ford - 2 doors, purple paint
    > - Nissan - 3 doors, red paint
    > Bike - 26 inch, green


    > - 27 inch, purple

    You could have a list of lists of maps of strings->integer.
    List<List<Map<String, Integer>> myStuff;

    Or, perhaps paying attention in you class would be a good idea.
    Daniel Pitts, Jan 17, 2007
    #2
    1. Advertising

  3. Ook

    Ook Guest


    > You could have a list of lists of maps of strings->integer.
    > List<List<Map<String, Integer>> myStuff;
    >
    > Or, perhaps paying attention in you class would be a good idea.
    >


    It would be if they actually taught this in my class LOL. Thanks for the
    info, I'll work with that.
    Ook, Jan 17, 2007
    #3
  4. Ook

    Stefan Ram Guest

    It was written:
    >Car - Chevy - 4 doors, green paint
    > - Ford - 2 doors, purple paint
    > - Nissan - 3 doors, red paint
    >Bike - 26 inch, green
    > - 27 inch, purple


    I would use Junotal to parse this into an object »room«:

    public class Main
    { public static void main( final java.lang.String[] args )
    {
    final de.dclj.ram.notation.unotal.RoomSource room =
    de.dclj.ram.notation.unotal.RoomFromModule.roomFrom
    (
    " " +
    " < < &car " +
    " Chevy=< doors=4 paint=green > " +
    " Ford=< doors=2 paint=purple > " +
    " Nissan=< doors=3 paint=red >> " +
    " < &bike " +
    " < inch=26 &green > " +
    " < inch=27 &purple >>> " +
    " " );

    java.lang.System.out.println
    ( room.getRoom( 0 ).getRoom( "Ford" ).get( "doors" ));

    java.lang.System.out.println
    ( room.getRoom( 0 ).getType() );

    java.lang.System.out.println
    ( room.getRoom( 1 ).hasType( "bike" ));

    for( final java.lang.Object o : room.getRoom( 1 ))
    java.lang.System.out.println( o ); }}

    The output of this program is:

    2
    car
    true
    < &green inch =26 >
    < &purple inch =27 >

    Junotal is the Java-Implementation of Unotal

    http://www.purl.org/stefan_ram/pub/unotal_en

    Junotal is available as part of ram.jar, an experimental
    library released in alpha state under the GPL.

    http://www.purl.org/stefan_ram/pub/ram-jar
    Stefan Ram, Jan 17, 2007
    #4
  5. Ook wrote:
    > Pardon the n00b question - How would you go about creating a list of
    > objects, where each object can contain a list of objects, and each
    > sub-object would have various properties(string, int)?
    >
    > This is an example. The parent object would be forms of transportation -
    > car, bike, etc. Each parent object has child objects (chevy, ford, etc.) and
    > each of these child objects has the same properties (number of doors,
    > color). How would I represent this data? I need some pointers to start me in
    > the right direction.
    >
    > Car - Chevy - 4 doors, green paint
    > - Ford - 2 doors, purple paint
    > - Nissan - 3 doors, red paint
    > Bike - 26 inch, green
    > - 27 inch, purple
    >


    I am going to concentrate on your use of the words "n00b", "parent
    object" and "child object" and therefore interpret your question
    somewhat differently than the other respondents ...

    class Transport {
    public Color color; // make it private, add getters & setters
    }

    class Car extends Transport {
    public int doors; // ditto
    }

    Then either ...

    class Chevy extends Car {
    Chevy() {
    super();
    super.doors = 4; // use setters instead
    color = Color.GREEN; // ditto
    }
    }

    or, more likely, ...

    Class CarExample {
    public static void main(String[] args) {
    Car chevy = new Car();
    chevy.doors = 4; // use setters instead
    chevy.color = Color.GREEN; // ditto
    }
    }

    The above is more typical of a learning exercise. The other respondents
    gave answers more typical of real applications.
    RedGrittyBrick, Jan 17, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Berchtel
    Replies:
    1
    Views:
    5,968
    John C. Bollinger
    May 20, 2005
  2. jimjim
    Replies:
    18
    Views:
    7,337
    Default User
    Apr 12, 2004
  3. 7stud
    Replies:
    11
    Views:
    671
    Dennis Lee Bieber
    Mar 20, 2007
  4. Nico Grubert
    Replies:
    1
    Views:
    327
    Peter Otten
    Jan 10, 2008
  5. mike
    Replies:
    1
    Views:
    313
    Mark Space
    Mar 3, 2009
Loading...

Share This Page