2013-06-11 54 views
1

我想將我的玩家的位置保存到一個列表中,這項工作很好,但現在如何通過搜索玩家名稱的列表來獲取我的位置?在其中創建列表類的代碼Java從列表中獲取整數和字符串的字符串

片和列表

public static class Character { 

    private String name; 

    private Location location; 
    public Character(String name, Location location) { 
     this.name = name; 
     this.location = location; 
    } 
} 

public static class Location { 
    private int x; 
    private int y; 
    private int z; 

    public Location(int x, int y, int z) { 
     this.x = x; 
     this.y = y; 
     this.z = z; 
    } 
} 

List<Character> rawInput = new ArrayList<Character>(); 

一段代碼,我的項目添加到列表:

else if (args[0].equalsIgnoreCase("select")) 
{ 
    int tmpX = (int)player.getLocation().getX(); 
    int tmpY = (int)player.getLocation().getY(); 
    int tmpZ = (int)player.getLocation().getZ(); 
    rawInput.add(
     new Character(player.getName(), new Location(tmpX, tmpY, tmpZ))); 
    player.sendMessage(
     ChatColor.GOLD + "[PlusCommands] " + ChatColor.GREEN 
     + "selected location set to player location!"); 
} 

這工作都很好,但我該怎麼辦獲取數據返回例如:

這是一個位置列表:玩家名稱XYZ:

PlayerThree 32 13 46

PlayerTwo 12 60 212

PlayerOne 43 62 523

所以我要搜索在該示例情況下,右側玩家我PlayerOne 因此,我想從數據其中字符串表示PlayerOne

在這種情況下,playerList那這一個:PlayerOne 43 62 523

如何做到這一點???

我希望我很清楚,如果沒有抱歉。

+1

在你的課堂上添加一個getter方法? – noMAD

+0

@noMAD getter方法? –

+0

'公共靜態位置getLocation(字符串名稱){//做東西來檢查名稱是否可用//獲取特定於名稱返回位置的位置; }' – noMAD

回答

3

在地方List<Character> rawInput = new ArrayList<Character>();使用Map<String,Character> rawInput = new LinkedHashMap<>();

添加球員:

rawInput.put(aNewCharacter.getName(), aNewCharacter); 

您應該檢查放的返回值:如果非空,已經使用的名稱。 閱讀的java.util.Map

的Javadoc要找到一個球員:

Character c = rawInput.get("PlayerOne"); // returns PlayerOne 43 62 523 
+0

就是這樣。地圖是java中使用最頻繁的數據結構之一。比ArrayList更經常的方法。但是,在使用自定義對象作爲鍵時,請小心,不要在這些自定義對象上實現equals()和hashcode()。進一步瞭解hashmaps以瞭解爲什麼。 –

+0

什麼是新特性?位置放在哪裏? –

+0

@StefanoL「地圖是java中使用最頻繁的數據結構之一,比數組列表更頻繁。」我不同意這一點。 –

1

您需要干將添加到這些類,像這樣:

package com.sandbox; 

public class Sandbox { 

    public static void main(String[] args) { 
     Character player = new Character("Foo", new Location(1, 2, 3)); 

     int tmpX = player.getLocation().getX(); 
     int tmpY = player.getLocation().getY(); 
     int tmpZ = player.getLocation().getZ(); 
    } 

    public static class Character { 

     private String name; 
     private Location location; 

     public Character(String name, Location location) { 
      this.name = name; 
      this.location = location; 
     } 

     public String getName() { 
      return name; 
     } 


     public Location getLocation() { 
      return location; 
     } 


    } 

    public static class Location { 
     private int x; 
     private int y; 
     private int z; 

     public Location(int x, int y, int z) { 
      this.x = x; 
      this.y = y; 
      this.z = z; 
     } 

     public int getX() { 
      return x; 
     } 

     public int getY() { 
      return y; 
     } 

     public int getZ() { 
      return z; 
     } 
    } 

} 

請留意我main的。它顯示你不需要投到(int)

+0

我在哪裏必須打電話給getName或getY等, –

+0

我想我必須去睡覺我正在受到腦損傷:P我嘗試學習,但我不明白我很抱歉浪費你的時間。 –

0
static Map<String,Location> nameAndLocation = new HashMap<String, Location>();  

public Character(String name, Location location) { 
      this.name = name; 
      this.location = location; 
      addToMap(this.name, this.location); 
     } 

public static void addToMap(String name, Location location) { 
    nameAndLocation.put(name,location); 
} 

public static Location getLocation(String name) { 
    return nameAndLocation.get(name); 
} 
0

這是一些相當凌亂的代碼。

您不應該創建自己的Location或Player類,因爲Bukkit已經包含了其中的一個。而不是自己動手,使用org.bukkit.util.Location + org.bukkit.entity.Player,你不應該有問題!

相關問題