Object List (help please)

Hey, I was wondering how I could add objects to an array list as they are created. I have a class that consists of a method that creates "Room" objects and as each one is created I need to add it to an array list:

publicstaticvoid createRooms()

{

Room

a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,

a2,b2,c2,d2,e2,f2,g2,h2,i2,j2,

a3,b3,c3,d3,e3,f3,g3,h3,i3,j3,

a4,b4,c4,d4,e4,f4,g4,h4,i4,j4,

a5,b5,c5,d5,e5,f5,g5,h5,i5,j5,

a6,b6,c6,d6,e6,f6,g6,h6,i6,j6,

a7,b7,c7,d7,e7,f7,g7,h7,i7,j7;

a1=new Room("a1");

a2=new Room("a2");

a3=new Room("a3");

a4=new Room("a4");

a5=new Room("a5");

a6=new Room("a6");

a7=new Room("a7");

b1=new Room("b1");

b2=new Room("b2");

b3=new Room("b3");

//etc.....

//and the room class:

publicclass Room

{

private String description;

private HashMap<String, Room> exits;

publicstatic Room currentRoom;

public ArrayList roomList =new ArrayList();

public Random numGEN =new Random();

public Room(String description)

{

this.description = description;

exits =new HashMap<String, Room>();

roomList.add(this);

}

What I need is for each room object's name to be added into the array list as a room object, so, roomList(0) = a1, roomList(1) = a2, etc...

Thanks

[2681 byte] By [sharokua] at [2007-11-27 4:50:12]
# 1
Hi,the way you are doing it is not working?...roomList.add(this);}
S_i_m_ua at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 2
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html http://java.sun.com/docs/books/tutorial/collections/
jverda at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 3

The links don't do much for me, I already know how to do arrays and array lists that is not the issue.

I did a test earlier to see if it would work and what it was putting in the array list and it returns something along the lines of "Room@36C2D98" which I assume is a memory location. Also when I trying to assign, say, roomList(0) to a Room variable like...

Room myRoom = roomList.get(0);

...I get an error saying that it was expecting type room.... Wait, Nevermind in the process of about to post code I figured out what was wrong...

I didn't put the <Room> in ArrayList<Room> roomList = new ArrayList();

Thanks anyways...

sharokua at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 4
I do have another question how ever on the same subject... When I do a System.out.println(roomList) it does not print out the list of rooms by there name (such as a1, a2, a3, etc...) it only prints out the memory location. How could I make it print out roomList by the name declaration?
sharokua at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 5

> I do have another question how ever on the same

> subject... When I do a System.out.println(roomList)

> it does not print out the list of rooms by there name

> (such as a1, a2, a3, etc...) it only prints out the

> memory location. How could I make it print out

> roomList by the name declaration?

public class Room {

...

public String toString() {

return description;

}

}

Hippolytea at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 6

> I already know how to

> do arrays and array lists

Based on this, I am included to disagree:

Room

a1,b1,c1,d1,e1,f1,g1,h1,i1,j1,

a2,b2,c2,d2,e2,f2,g2,h2,i2,j2,

a3,b3,c3,d3,e3,f3,g3,h3,i3,j3,

a4,b4,c4,d4,e4,f4,g4,h4,i4,j4,

a5,b5,c5,d5,e5,f5,g5,h5,i5,j5,

a6,b6,c6,d6,e6,f6,g6,h6,i6,j6,

a7,b7,c7,d7,e7,f7,g7,h7,i7,j7;

a1=new Room("a1");

a2=new Room("a2");

a3=new Room("a3");

a4=new Room("a4");

... etc. ...

jverda at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 7

Populating an ArrayList with anonymous objects is done like this:

ArrayList<Room> list = new ArrayList<Room>();

list.add(new Room("a1"));

list.add(new Room("a2"));

Psybera at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...
# 8

> Populating an ArrayList with anonymous objects is

> done like this:

>

> > ArrayList<Room> list = new ArrayList<Room>();

> list.add(new Room("a1"));

> list.add(new Room("a2"));

>

we've been over this w/ this poster before.

petes1234a at 2007-7-12 10:03:28 > top of Java-index,Java Essentials,New To Java...