Java从列表中获取带有整数和字符串的字符串

时间:2013-06-11 22:40:44

标签: java list class bukkit

我正在尝试将玩家在我的世界中的位置保存到列表中,但是现在如何通过在playerName上搜索列表来获取我的位置?

创建列表类的代码片段和列表

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!");
}

这一切都很好,但我如何获取数据,例如:

这是一个包含位置的列表:Playername X Y Z:

PlayerThree 32 13 46

PlayerTwo 12 60 212

PlayerOne 43 62 523

所以我想在这个例子中搜索合适的玩家我是PlayerOne 所以我想从playerList获取数据,其中字符串显示为PlayerOne

在这种情况下就是这个:PlayerOne 43 62 523

我该怎么做?

如果没有,我希望我很清楚。

4 个答案:

答案 0 :(得分:3)

取代List<Character> rawInput = new ArrayList<Character>();使用Map<String,Character> rawInput = new LinkedHashMap<>();

添加播放器:

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

您应该检查put的返回值:如果为非null,则表示该名称已被使用。 阅读java.util.Map

的Javadoc

找一名球员:

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

答案 1 :(得分:1)

您需要将getter添加到这些类中,如下所示:

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)

答案 2 :(得分: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);
}

答案 3 :(得分:0)

这是一些非常混乱的代码。

您不应该创建自己的Location或Player类,因为Bukkit已经包含其中一个。使用org.bukkit.util.Location + org.bukkit.entity.Player而不是制作自己的,而不应该有问题!