创建建筑物时的 Arraylist
Arrary list while creating a buildin
package uk.ac.reading.Nischal.buidlingconsole;
import java.util.ArrayList;
public class Building {
private int xSize = 10; // size of building in x
private int ySize = 10; // and y
private static String test;
StringSplitter bigBox;
private static ArrayList<Room> allRooms; // array of rooms
public Building(String build) {
setBuilding(build);
}
public void setBuilding(String bS) {
// allRooms.clear();
allRooms= new ArrayList<Room>();
bigBox = new StringSplitter(bS, ";");
String first = (bigBox.getNth(0, ""));
StringSplitter sizeXY = new StringSplitter(first, " ");
xSize = Integer.parseInt(sizeXY.getNth(0, ""));
ySize = Integer.parseInt(sizeXY.getNth(1, ""));
//test = bigBox.getNth(3, "");
for(int i = 0; i < bigBox.numElement(); i++){
String r = (bigBox.getNth(i, ""));
allRooms.add(new Room(r));
}
}
public String toString(){
String res = "Building size " + xSize + "," + ySize + "\n" ;
String s = "";
for (Room r : allRooms)
s = s + r.toString() + "\n";
return res + s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Building b = new Building("11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5"); // create
System.out.println(b.toString());
System.out.println(test);
}
}
我正在尝试通过传递以下形式的字符串来创建建筑物,
11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5
但我遇到了未知错误
我被告知我必须使用数组列表,在这里我尝试这样做但我得到了奇怪的错误。
我希望我做对了,这个 StringSplitter 的功能与 String 的拆分方法几乎相同。
由于 bigBox 的第 0 个元素包含 xSize 和 ySize,我认为您的循环应该从 1 而不是 0 开始。
for(int i = 1; i < bigBox.numElement(); i++){
String r = (bigBox.getNth(i, ""));
allRooms.add(new Room(r));
}
如果能显示为错误日志会很有帮助。
package uk.ac.reading.Nischal.buidlingconsole;
import java.util.ArrayList;
public class Building {
private int xSize = 10; // size of building in x
private int ySize = 10; // and y
private static String test;
StringSplitter bigBox;
private static ArrayList<Room> allRooms; // array of rooms
public Building(String build) {
setBuilding(build);
}
public void setBuilding(String bS) {
// allRooms.clear();
allRooms= new ArrayList<Room>();
bigBox = new StringSplitter(bS, ";");
String first = (bigBox.getNth(0, ""));
StringSplitter sizeXY = new StringSplitter(first, " ");
xSize = Integer.parseInt(sizeXY.getNth(0, ""));
ySize = Integer.parseInt(sizeXY.getNth(1, ""));
//test = bigBox.getNth(3, "");
for(int i = 0; i < bigBox.numElement(); i++){
String r = (bigBox.getNth(i, ""));
allRooms.add(new Room(r));
}
}
public String toString(){
String res = "Building size " + xSize + "," + ySize + "\n" ;
String s = "";
for (Room r : allRooms)
s = s + r.toString() + "\n";
return res + s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Building b = new Building("11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5"); // create
System.out.println(b.toString());
System.out.println(test);
}
}
我正在尝试通过传递以下形式的字符串来创建建筑物,
11 11;0 0 5 5 3 5;6 0 10 10 6 6;0 5 5 10 2 5
但我遇到了未知错误
我被告知我必须使用数组列表,在这里我尝试这样做但我得到了奇怪的错误。
我希望我做对了,这个 StringSplitter 的功能与 String 的拆分方法几乎相同。
由于 bigBox 的第 0 个元素包含 xSize 和 ySize,我认为您的循环应该从 1 而不是 0 开始。
for(int i = 1; i < bigBox.numElement(); i++){
String r = (bigBox.getNth(i, ""));
allRooms.add(new Room(r));
}
如果能显示为错误日志会很有帮助。