我的代码未向ArrayList
object[]
的{{1}}添加新索引。
我的代码:
System.out.println("started1");
ArrayList<Object[]> toSend = new ArrayList<Object[]>();
System.out.println("started2");
for(Entry<String, Room> map : rooms.entrySet())
{
System.out.println("started3");
String key = map.getKey();
Room val = map.getValue();
System.out.println("started3.1");
Player owner = players.get(val.getOwnerID());
System.out.println("started3.125");
Object[] toAdd = new Object[] {key, val.getRoomName(), val.getCurrentPlayerSize(), val.getMaxPlayers(), owner.getName()};
System.out.println("started3.15");
toSend.add(toAdd);
System.out.println("started3.2");
}
System.out.println("started4");
client.sendEvent("refreshAvailableRooms", toSend);
System.out.println("started5");
设置owner
变量以链接到Player
类。我已经尝试打印出类值,它们都很好。当我尝试向我的对象数组添加新的Object[]
时,它就会停止。
可能出现什么问题? (在控制台中,它会在System.out.println(started3.125");
编辑:我应该提到rooms
(在for循环中)链接到HashMap:
private static HashMap<String, Player> players = new HashMap<String, Player>();
private static HashMap<String, Room> rooms = new HashMap<String, Room>();
EDIT2:RUNNABLE CODE
Room.java:
public class Room {
public String RoomName = "Room Name";
public int size = 3;
public int max = 10;
public String ownerID = "BobSmith";
public String ID = "Room1";
public Room() { }
}
Player.java:
public class Player {
public String ID = "BobSmith";
public String Name = "Bob";
public Player() { }
}
Main.java:
public class main {
private static HashMap<String, Player> players = new HashMap<String, Player>();
private static HashMap<String, Room> rooms = new HashMap<String, Room>();
public static void main(String args[]) {
players.put("BobSmith", new Player());
rooms.put("Room1", new Room());
rooms.put("Room2", new Room());
rooms.put("Room3", new Room());
System.out.println("started1");
ArrayList<Object[]> toSend = new ArrayList<Object[]>();
System.out.println("started2");
for(Entry<String, Room> map : rooms.entrySet())
{
System.out.println("started3");
String key = map.getKey();
Room val = map.getValue();
System.out.println("started3.1");
Player owner = players.get(val.ownerID);
System.out.println("started3.125");
Object[] toAdd = new Object[] {key, val.RoomName, val.size, val.max, owner.Name};
System.out.println("started3.15");
toSend.add(toAdd);
System.out.println("started3.2");
}
System.out.println("started4");
}
}