Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to: create a list of objects?

Reply
Thread Tools

How to: create a list of objects?

 
 
Ook
Guest
Posts: n/a
 
      01-17-2007
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




 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      01-17-2007

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.

 
Reply With Quote
 
 
 
 
Ook
Guest
Posts: n/a
 
      01-17-2007

> 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.


 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      01-17-2007
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.roomFro m
(
" " +
" < < &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

 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      01-17-2007
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to create a big list of list Mۤv@ Python 1 06-06-2009 02:20 AM
How do I create a Drop Down List with Hyperlinks as List Items in ASP.NET Tony Tone ASP .Net 2 04-28-2006 07:33 PM
How do I create a Drop Down List with Hyperlinks as List Items in ASP.NET Tony Tone ASP .Net 0 04-28-2006 06:38 PM
Create a list of list in Stl yinglcs@gmail.com C++ 9 01-11-2006 10:33 PM



Advertisments