【发布时间】:2014-04-27 01:37:49
【问题描述】:
我正在创建一个对象数组,称为房间。
static Room [] rooms = new Room [3];
//populate the array.
rooms[0] = new Room ("Bedroom", "This is your bedroom." );
rooms[1] = new Room("Hallway", "This is the hallway of your house.");
//constructor
static String room = "";
static String descriptionOfTheRoom = "";
public Room ( String newRoom, String newDescriptionOfTheRoom ){
room = newRoom;
descriptionOfTheRoom = newDescriptionOfTheRoom;
}
// get room method
public String getRoom (){
return room;
}
// when I try to get the room.
System.out.println("Room" + r[i].getRoom() );
它会打印出最后添加到数组中的房间。所以总是打印走廊。 那么我怎样才能打印每个元素呢?还是访问每个元素?
提前感谢大家。
【问题讨论】: