0

我想知道我是否可以在数组内有一个数组?我想不出办法来解释它。

        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 阶梯中为每个玩家创建一个新数组。除了制作 48 个 if 语句之外,还有一种方法可以更改 coords 数组的变量名称,以将其放入一个 for 循环中,其中计数器递增数组的名称。嗯,这是一种令人困惑的解释方式,但这是我能做的最好的。

4

3 回答 3

2

简短的回答是肯定的,您可以声明一个类似的数组String[][] playerCoords,但是我建议查看Map界面,因为它可能更能描述您想要做什么。

于 2012-05-01T20:09:59.720 回答
1

我不太确定我是否正确理解您,但您可能想要做一些类似二维数组的事情。看看这里:

http://www.go4expert.com/forums/showthread.php?t=1162

它是这样分配的:

double[][] a2 = new double[10][5];
于 2012-05-01T20:08:08.613 回答
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);
}

您的配置变量将被命名为 Tribute_1_spawn、Tribute_2_spawn 等等......如果您需要进一步使用 Player 对象,您可以将它们保存在数组或其他数据结构中。

于 2012-05-01T20:19:52.873 回答