奇数返回ArrayList#size()

时间:2016-09-08 20:23:43

标签: java arraylist random

我想在ArrayList中获取一个随机HashMap,当我查看HashMap中的所有数组列表时,所有内容的大小都应该是2。

当我尝试将ArrayList中的Hashmap添加到另一个时,会出现问题。然后它将大小返回为0,即使HashMap中所有列表的大小都是2.有人知道为什么吗?

for(Point p : defensePossibilities.keySet())
    System.out.println(defensePossibilities.get(p).size());

ArrayList<Point> points = new ArrayList<>();
while(points == null) {
    try {
        int random = rnd.nextInt(defensePossibilities.size());
        points.addAll(
                defensePossibilities.get(random));
    } catch(Exception e) {}
}
System.out.println("PointsSize: " + points.size());

int piece2 = rnd.nextInt(points.size());

这是它在stacktrace中返回的内容

2
2
2
2
2
2
2
2
PointsSize: 0
Exception in thread "Thread-0" java.lang.IllegalArgumentException: bound must be positive
    at java.util.Random.nextInt(Unknown Source)
    at me.xthegamerplayz.FirstGame.board.White_AI.move(White_AI.java:95)
    at me.xthegamerplayz.FirstGame.board.ChessBoard.tick(ChessBoard.java:29)
    at me.xthegamerplayz.FirstGame.board.ChessBoard.<init>(ChessBoard.java:21)
    at me.xthegamerplayz.FirstGame.Game.render(Game.java:124)
    at me.xthegamerplayz.FirstGame.Game.run(Game.java:70)
    at java.lang.Thread.run(Unknown Source)

第95行

int piece2 = rnd.nextInt(

如果points中的所有ArrayLists的大小都是2,为什么HashMap的大小为0?

3 个答案:

答案 0 :(得分:4)

points == null替换为points.isEmpty()

points变量不为null,因为您在while循环之前为points变量指定了一个arraylist。我假设您要做的是在poinst列表为空时检查。因此,进行建议的更改将解决您的问题。

for(Point p : defensePossibilities.keySet())
    System.out.println(defensePossibilities.get(p).size());

ArrayList<Point> points = new ArrayList<>();
while(points == null) { // points is not null, it is just empty
// replace points == null with points.isEmpty()
    try {
        int random = rnd.nextInt(defensePossibilities.size());
        points.addAll(
                defensePossibilities.get(random));
    } catch(Exception e) {}
}
System.out.println("PointsSize: " + points.size());

int piece2 = rnd.nextInt(points.size());

答案 1 :(得分:1)

点不是null因为你已经实例化了它 ArrayList<Point> points;这是空的 所以替换你的代码

for(Point p : defensePossibilities.keySet())
    System.out.println(defensePossibilities.get(p).size());

ArrayList<Point> points = new ArrayList<>();
while(points.isEmpty()) {
    try {
        int random = rnd.nextInt(defensePossibilities.size());
        points.addAll(
                defensePossibilities.get(random));
    } catch(Exception e) {}
}
System.out.println("PointsSize: " + points.size());

int piece2 = rnd.nextInt(points.size());

更新
改变

points.addAll(defensePossibilities.keySet());

2更新

  

public V get(Object key)返回指定键的值   映射,如果此映射不包含键的映射,则为null。

所以

ArrayList<Point> a = new ArrayList<>();
a.addAll(defensePossibilities.keySet());
int random = rnd.nextInt(defensePossibilities.size());
points.add(a.get(random))

答案 2 :(得分:0)

在使用isEmpty()而不是== null之后,您的实际问题是ArrayIndexOutOfBoundsException。

在您的代码中,random的值可以与List的大小一样大。假设你里面有10个对象,随机是10:

defensePossibilities.get(随机); // 10 object =索引0到9,但不是10!

请记住,列表与数组一样,开始按0索引。

} catch(Exception e) {
    System.out.println(e.toString());    
}

你会明白我的意思。 ; O)