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

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

生成部分:

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

基本上我想做的是,如果这些坐标已经被占用,则重新生成坐标。

  1. 我无法真正将这些坐标存储到一个大数组列表中,因为我并不总是希望此功能发挥作用,例如,如果我的玩家数量多于 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,则重新生成它,否则移动玩家并将坐标添加到列表中。

但似乎我做错了什么,因为我收到此错误:错误:找不到包含(int,int)的合适方法

我该怎么做?

最佳答案

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

Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (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);
}

关于Java arraylist - 检查在从列表生成随机整数时是否设置了 arraylist 索引中的两项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18055978/

相关文章:

Java 递归通过 ArrayList 中的引用传递

java - Kafka Connect 无法找到已开发插件的类

java - List < Class < 是什么意思?扩展数据类型>>?

Java函数查找素数不起作用

Java 元组/对

c# - 在 View 状态中存储 List<int>

java - 为什么子列表不适用于 List<Object>?

java - Android 上生成的不同 RSA 公钥

java - lucene 的总点击次数

java - 将运算符从 double 转换回 char 后,如何使用它?