Java在从列表生成随机int时检查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}
    };

生成部分:

    int random = Misc.random(coords.length - 1);
    handler.move(coords[random][0], coords[random][1], 0);

基本上,我想做的是,如果这些坐标已经取下,那么重新生成坐标。

  1. 我真的不能把这些coords存储到一个大的数组列表中,因为我并不总是希望这个功能发挥作用,例如,如果我的玩家比coords[]数组中的项目多,那么这个功能就不会被激活

因此,我创建了一个使用coords的数组列表,这是我关于如何做到这一点的逻辑:

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

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

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

我该怎么做

ArrayList#containsObject与列表中包含的对象进行比较。。。

如果此列表包含指定的元素,则返回true。更多形式上,当且仅当此列表至少包含一个元素e,使得(o==null?e==null:o.equals(e)).

您的列表包含int的数组,这些数组不容易进行比较。

如果使用Object,其equals方法能够比较其他类似对象的坐标,则会容易得多。类似。。。

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;
    }
}

然后你可以用更像。。。

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

最新更新