Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java从带有整数和字符串的列表中获取字符串_Java_List_Class_Bukkit - Fatal编程技术网

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

Java从带有整数和字符串的列表中获取字符串,java,list,class,bukkit,Java,List,Class,Bukkit,我正在尝试将玩家在minecraft中的位置保存到一个列表中这很有效,但是现在如何通过搜索playerName上的列表来恢复我的位置 创建list类和list public static class Character { private String name; private Location location; public Character(String name, Location location) { this.name = name;

我正在尝试将玩家在minecraft中的位置保存到一个列表中这很有效,但是现在如何通过搜索playerName上的列表来恢复我的位置

创建list类和list

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>();
这一切都很好,但如何取回数据,例如:

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

播放者三32 13 46

玩家二12 60 212

播放员4362523

所以我想在这个例子中寻找合适的玩家,我是玩家 所以我想从playerList中获取数据,字符串上写着PlayerOne

在这种情况下,这是一个:玩家43 62 523

我该怎么做

我希望我足够清楚,如果不清楚,我对此表示歉意。

代替
List rawInput=new ArrayList()使用
映射rawInput=new LinkedHashMap()

要添加玩家,请执行以下操作:

rawInput.put( aNewCharacter.getName(), aNewCharacter );
Character c = rawInput.get( "PlayerOne" ); // returns PlayerOne 43 62 523
您应该检查put的返回值:如果非null,则名称已被使用。 阅读

要查找玩家,请执行以下操作:

rawInput.put( aNewCharacter.getName(), aNewCharacter );
Character c = rawInput.get( "PlayerOne" ); // returns PlayerOne 43 62 523
代替
List rawInput=new ArrayList()使用
映射rawInput=new LinkedHashMap()

要添加玩家,请执行以下操作:

rawInput.put( aNewCharacter.getName(), aNewCharacter );
Character c = rawInput.get( "PlayerOne" ); // returns PlayerOne 43 62 523
您应该检查put的返回值:如果非null,则名称已被使用。 阅读

要查找玩家,请执行以下操作:

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

您需要向这些类添加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)

您需要向这些类添加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)

staticmap name和location=newhashmap();
公共字符(字符串名称、位置){
this.name=名称;
这个位置=位置;
addToMap(this.name,this.location);
}
公共静态void addToMap(字符串名称、位置){
name和location.put(名称、位置);
}
公共静态位置getLocation(字符串名称){
返回name和location.get(name);
}
静态映射名称和位置=新HashMap();
公共字符(字符串名称、位置){
this.name=名称;
这个位置=位置;
addToMap(this.name,this.location);
}
公共静态void addToMap(字符串名称、位置){
name和location.put(名称、位置);
}
公共静态位置getLocation(字符串名称){
返回name和location.get(name);
}

这是一些相当混乱的代码


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

这是一些相当混乱的代码


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

在类中添加一个getter方法?@noMAD getter method?
公共静态位置getLocation(字符串名称){//检查名称是否可用//获取特定于名称返回位置的位置;}
使用映射而不是数组列表。在类中添加一个getter方法?@noMAD getter method?
公共静态位置getLocation(字符串名称){//Do stuff to check name是否可用//Get location specific to name return location;}
使用映射而不是arraylist.Yap就是这样。映射是java中使用最频繁的数据结构之一。比arraylist更为常见。但是在使用自定义对象作为键并且不实现equals()和hashcode()时要小心在这些自定义对象上。进一步阅读hashmaps了解原因。什么是一个wcharacter?位置放在哪里?@StefanoL“Maps是java中使用最频繁的数据结构之一。比arraylist更频繁。”我不同意。是的,就是这样。映射是java中使用最频繁的数据结构之一。比arraylist更频繁。但是在使用自定义对象作为键时,不要实现equals()和hashcode(),要小心在这些自定义对象上。进一步阅读hashmaps了解原因。什么是一个wcharacter?位置放在哪里?@StefanoL“Maps是java中使用最频繁的数据结构之一。比arraylist更频繁。”我不同意。我应该在哪里叫getName或getY等,我想我必须去睡觉我的大脑受到了损害:P我试图学习,但我不明白,我很抱歉浪费了你的时间。我应该在哪里叫getName或getY等,我想我必须去睡觉我的大脑受到了损害:P我试图学习,但我不明白,我很抱歉没有浪费你的时间。