Java arraylist - 检查是否在从列表生成随机整数时设置了arraylist索引中的两个项目?

Jun*_*ded 0 java arraylist

我的坐标列表:

    int[][] coords = {
       {3093, 3630 }, {3095, 3632}, {3098, 3633},
       {3101, 3633 }, {3104, 3631}, {3106, 3629}, 
       {3107, 3627}, {3108, 3624}, {3109, 3620},
       {3108, 3617}, {3106, 3614}, {3102, 3613},
       {3099, 3613}, {3097, 3613}, {3093, 3614},
       {3090, 3617}, {3087, 3619}
    };
Run Code Online (Sandbox Code Playgroud)

生成部分:

    int random = Misc.random(coords.length - 1);
    handler.move(coords[random][0], coords[random][1], 0);
Run Code Online (Sandbox Code Playgroud)

基本上我要做的是,如果已经采用这些坐标,则重新生成坐标.

  1. 我无法将这些coords存储到一个大的arraylist中,因为我并不总是希望这个功能能够运行,例如,如果我有更多的玩家而不是coords []数组中的项目,那么这将不会被激活.

因此我创建了一个使用过的coords的arraylist,这是我如何做到这一点的逻辑:

    int[][] coords = {
       {3093, 3630 }, {3095, 3632}, {3098, 3633},
       {3101, 3633 }, {3104, 3631}, {3106, 3629}, 
       {3107, 3627}, {3108, 3624}, {3109, 3620},
       {3108, 3617}, {3106, 3614}, {3102, 3613},
       {3099, 3613}, {3097, 3613}, {3093, 3614},
       {3090, 3617}, {3087, 3619}
    };

    ArrayList<Integer> coordinates = new ArrayList<Integer>();

    int random = Misc.random(coords.length - 1);

    if (getPlayersCount() < coords.length) {
        if (coordinates.contains(coords[random][0], coords[random][1])) {
            random = Misc.random(coords.length - 1);
        }
        else {
            handler.move(coords[random][0], coords[random][1], 0);
            coords.add(coords[random][0], coords[random][1]);
        }
    }
    else {
        random = Misc.random(coords.length - 1);
        handler.move(coords[random][0], coords[random][1], 0);
    }
Run Code Online (Sandbox Code Playgroud)

基本上,如果玩家的数量少于阵列长度,则进程.如果坐标arraylist包含生成的X,Y,则重新生成它,否则移动播放器并将坐标添加到列表中.

但似乎我做错了什么,因为我得到了这个错误: error: no suitable method found for contains(int,int)

我怎样才能做到这一点?

Mad*_*mer 7

ArrayList#contains将a Object与列表中包含的对象进行比较...

如果此列表包含指定的元素,则返回true.更正式地,当且仅当此列表包含至少一个元素e时才返回true(o == null?e == null:o.equals(e)).

您的列表包含int不易比较的数组.

如果您使用Objectequals方法能够比较其他类似对象的坐标,那将会容易得多.就像是...

public class Coordinate {
    private int latitude;
    private int longitude;

    public Coordinate(int latitude, int longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
    }

    public int getLatitude() {
        return latitude;
    }

    public int getLongitude() {
        return longitude;
    }

    public boolean equals(Object value) {
        boolean equals = false;
        if (value instanceof Coordinate) {
            Coordinate coord = (Coordinate) value;
            equals = getLatitude() == coord.getLatitude() && getLongitude() == coord.getLongitude();
        }
        return equals;
    }

    //good practice to override hashcode when you override equals
    public int hashcode() {
        int hash = 7;
        hash = 89 * hash + this.latitude;
        hash = 89 * hash + this.longitude;
        return hash;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以使用更像......

Coordinate[] coords = {
   new Coordinate(3093, 3630 ), new Coordinate(3095, 3632), new Coordinate(3098, 3633),
   new Coordinate(3101, 3633 ), new Coordinate(3104, 3631), new Coordinate(3106, 3629), 
   new Coordinate(3107, 3627), new Coordinate(3108, 3624), new Coordinate(3109, 3620),
   new Coordinate(3108, 3617), new Coordinate(3106, 3614), new Coordinate(3102, 3613),
   new Coordinate(3099, 3613), new Coordinate(3097, 3613), new Coordinate(3093, 3614),
   new Coordinate(3090, 3617), new Coordinate(3087, 3619)
};

ArrayList<Coordinate> coordinates = new ArrayList<Coordinate>();

int random = Misc.random(coords.length - 1);

if (getPlayersCount() < coords.length) {
    Coordinate coord = new Coordinate(coords[random].getLatitude(), coords[random].getLongitude());
    if (coordinates.contains(coord)) {
        random = Misc.random(coords.length - 1);
    }
    else {
        handler.move(coords[random].getLatitude(), coords[random].getLongitude(), 0);
        coordinates.add(coords[random]);
    }
}
else {
    random = Misc.random(coords.length - 1);
    handler.move(coords[random].getLatitude(), coords[random].getLongitude(), 0);
}
Run Code Online (Sandbox Code Playgroud)