我想知道我是否可以在数组中有一个数组?我想不出解释它的方法。
if (numOfPlayers >= 2) {
this.config.getString("Tribute_one_spawn");
String[] onecoords = this.config.getString("Tribute_one_spawn").split(",");
Player Tribute_one = (Player)this.Playing.get(0);
World w = p.getWorld();
double x = Double.parseDouble(onecoords[0]);
double y = Double.parseDouble(onecoords[1]);
double z = Double.parseDouble(onecoords[2]);
Location oneloc = new Location(w, x, y, z);
Tribute_one.teleport(oneloc);
this.Frozen.add(Tribute_one);
Tribute_one.setFoodLevel(20);
this.config.getString("Tribute_two_spawn");
String[] twocoords = this.config.getString("Tribute_two_spawn").split(",");
Player Tribute_two = (Player)this.Playing.get(1);
World twow = p.getWorld();
double twox = Double.parseDouble(twocoords[0]);
double twoy = Double.parseDouble(twocoords[1]);
double twoz = Double.parseDouble(twocoords[2]);
Location twoloc = new Location(twow, twox, twoy, twoz);
Tribute_two.teleport(twoloc);
this.Frozen.add(Tribute_two);
Tribute_two.setFoodLevel(20);
}
if (numOfPlayers() >= 3) {
this.config.getString("Tribute_three_spawn");
String[] coords = this.config.getString("Tribute_three_spawn").split(",");
Player Tribute_three = (Player)this.Playing.get(2);
World w = p.getWorld();
double x = Double.parseDouble(coords[0]);
double y = Double.parseDouble(coords[1]);
double z = Double.parseDouble(coords[2]);
Location loc = new Location(w, x, y, z);
Tribute_three.teleport(loc);
this.Frozen.add(Tribute_three);
Tribute_three.setFoodLevel(20);
}
正如您所看到的,我必须在每个玩家的if else梯形图中创建一个新数组。有没有一种方法可以改变coords数组的变量名称,将其置于for循环中,并使用计数器递增数组名称。那是解释它的一种令人困惑的方式,但这是我能做的最好的事情。
答案 0 :(得分:2)
简短的回答是肯定的,您可以声明一个类似String[][] playerCoords
的数组,但是我建议您查看Map
界面,因为它可能更能描述您想要做什么。
答案 1 :(得分:1)
我不太确定我是否理解正确,但您可能想要做一些像二维数组的事情。看看这里:
http://www.go4expert.com/forums/showthread.php?t=1162
它是这样分配的:
double[][] a2 = new double[10][5];
答案 2 :(得分:0)
只要您可以重命名配置变量,就可以将代码换成for循环:
for (int i=1;i<numOfPlayers;i++) {
String[] onecoords = this.config.getString().split(",");
Player Tribute_i = (Player)this.Playing.get(i-1);
World w = p.getWorld();
double x = Double.parseDouble(onecoords[0]);
double y = Double.parseDouble(onecoords[1]);
double z = Double.parseDouble(onecoords[2]);
Location oneloc = new Location(w, x, y, z);
Tribute_i.teleport(oneloc);
this.Frozen.add(Tribute_i);
Tribute_i.setFoodLevel(20);
}
Yor配置变量将被命名为Tribute_1_spawn,Tribute_2_spawn等等...... 如果需要进一步使用Player对象,可以将它们保存在数组或其他数据结构中。