Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

How to: create a list of objects?

Thread view: 
Ook - 17 Jan 2007 00:11 GMT
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
Daniel Pitts - 17 Jan 2007 00:37 GMT
> 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
[quoted text clipped - 10 lines]
>     - 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.
Ook - 17 Jan 2007 01:46 GMT
> 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.
Stefan Ram - 17 Jan 2007 04:13 GMT
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
RedGrittyBrick - 17 Jan 2007 20:00 GMT
> 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
[quoted text clipped - 11 lines]
> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.