How to: create a list of objects?

O

Ook

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
 
D

Daniel Pitts

Ook said:
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.
 
O

Ook

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

Stefan Ram

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
 
R

RedGrittyBrick

Ook said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top