2012-05-01 41 views
1

我想知道如果我可以有一個數組內的數組?我想不出一個解釋它的方法。Java:數組內的數組?

 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循環中,使用計數器遞增數組的名稱,而不是使用48 if語句。那是一個令人困惑的解釋方式,但這是我能做的最好的。

+3

雖然完全有可能擁有一組數組,但通常更好的做法是創建一個新類以更準確地表示數據。 –

+0

你將不得不努力尋找更好的方式來解釋你的問題。 1行問題描述和20行代碼不是一個好問題,並且會有更多類似評論或遍佈整個地方的答案。請縮小範圍。 –

回答

2

簡短的回答是肯定的,你可以聲明數組一樣String[][] playerCoords,不過我建議看Map接口,因爲它可能更能描述你想要做什麼。

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對象,可以將它們保存在數組或其他數據結構中。