【发布时间】:2014-06-16 07:13:27
【问题描述】:
所以我一直在研究一个小型坦克程序,但每当我尝试运行它时,我都会在第 477 行 (http://pastebin.com/k4WNXE6Q) 收到 ArrayIndexOutOf BoundsException。
void placeStations(int Number) {
for (int i = 0; i<Number; i++) {
stations.add(new RefillStation(int(random(0, width)), int(random(0, height))));
// This line of code refuses to work. I get an 'ArrayIndexOutOfBoundsException: -3'
RefillStation station = stations.get(i);
for (Tank tank : tanks) {
if (station.getRectangle().intersects(tank.getRectangle())) {
station.kill();
i=i-1;
}
}
for (Obstacle obstacle : obstacles) {
if (station.getRectangle().intersects(obstacle.getRectangle())) {
station.kill();
i=i-1;
}
}
}
}
我已经尝试了几个小时来查找错误,但我看不出与上面的方法没有什么不同,这似乎工作正常。我在某些地方使用 'i' 类型的循环,因为每当我尝试从现代 for 循环中的索引中删除某些内容时,它都会给我一个大小更改异常。关于我可以做些什么来解决这个问题的任何想法?
【问题讨论】:
标签: java arrays processing indexoutofboundsexception