【发布时间】:2016-12-04 20:32:12
【问题描述】:
每次运行以下方法时,我都会收到错误消息。即使列表不为空,错误仍然存在。
public Position getBestMove() {
int max = -10000;
int best = -1;
System.out.println("CALLED");
System.out.println(successorEvaluations.size());
// iterate over successors and return the one with the highest eval result
for (int i = 0; i < successorEvaluations.size(); i++) {
if (max < successorEvaluations.get(i).score) {
max = successorEvaluations.get(i).score;
best = i;
}
}
return successorEvaluations.get(best).pos;
}
错误输出:
java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Board.getBestMove(Board.java:151)
【问题讨论】:
-
您的应用程序是否在多个线程中运行,并且可以从另一个线程更改 successorEvaluations 吗?如果只有一个线程,我在您的代码中看不到任何会导致 ArrayOutOfBoundsException 的内容。
-
您确定将
max初始化为-10000是正确的吗?您预计successorEvaluations.get(i).score的值范围是多少? -
您要测试的这个数组的内容是什么?
标签: java arraylist error-handling indexoutofboundsexception