您好我正在尝试循环播放arraylist但收到错误消息。所以我非常具体的问题是,我在哪里做错了什么?
错误讯息:
线程中的异常" main" java.util.ConcurrentModificationException at java.util.ArrayList $ Itr.checkForComodification(ArrayList.java:901) at java.util.ArrayList $ Itr.next(ArrayList.java:851) 在main.numberOfNeighbours(main.java:130) 在main.calculateNextGen(main.java:95) 在main.main(main.java:30)
代码:
public static int numberOfNeighbours(boolean[][] arena, int x, int y){
//Calculates a given cells number of neighbours.
int height = arena[0].length;
int width = arena.length;
ArrayList<int[]> cors = new ArrayList<int[]>();
for(int i = -1; i < 2; i++){
for(int e = -1; e < 2; e++){
int[] xy = {x+i, y+e};
cors.add(xy);
}
}
//This is where I get the error
for(int[] xy : cors){
//Exclude cors that are out of range or the cell itself.
if(xy[0] == -1 || xy[1] == -1 || xy[0] == width || xy[0] == height || (xy[0] == x && xy[1] == y)){
int index = cors.indexOf(xy);
cors.remove(index);
}
}
int neighbours = 0;
for(int[] xy : cors){
if(arena[xy[0]][xy[1]]){
neighbours++;
}
}
return neighbours;
}
我一直在谷歌搜索,但无法找到解决方案。我认为我的主要问题是我不理解错误信息,所以任何帮助解释都会很棒。
答案 0 :(得分:1)