我有一个类称为单元并在它我有一个运行这段代码的更新方法:如果语句忽略返回方法,其他解决方案?
if(goalReached){
if(returnNearestCell() > -1 && isTarget && this.checkCollide(cells.get(returnNearestCell()).x, cells.get(returnNearestCell()).y, cells.get(returnNearestCell()).mass)){
addMass(cells.get(returnNearestCell()).mass);
cells.get(returnNearestCell()).mass = 20;
cells.get(returnNearestCell()).x = (int) Math.floor(Math.random() * 1001);
cells.get(returnNearestCell()).y = (int) Math.floor(Math.random() * 701);
isTarget = false;
}
if(returnNearestCell() > -1 && !isTarget){
goalX = cells.get(returnNearestCell()).x;
goalY = cells.get(returnNearestCell()).y;
target = cells.indexOf(returnNearestCell());
isTarget = true;
}else if(returnNearestCell() == -1){
goalX = (int) Math.floor(Math.random() * 1001);
goalY = (int) Math.floor(Math.random() * 701);
isTarget = false;
}
if(!isTarget){
addMass(5);
}
goalReached = false;
}
基本上概括起来讲,每个小区查找与一更小的质量,并且如果最近的细胞细胞,然后将goalX和goalY设置为该细胞的位置。如果没有发现具有相同标准的这样的单元格,则只需转到随机位置。代码工作正常,直到由于某种原因,第一个if语句被忽略:
returnNearestCell() > -1
然后我得到一个ArrayIndexOutOfBoundsException。
我returnNearestCell方法进行如下:
public int returnNearestCell(){
int x = 0;
int distance = 9999999;
int min = distance;
for(Cell cell : cells){
if(this != cell){
distance = (int)Math.sqrt((this.x - cell.x)*(this.x - cell.x) + (cell.y - this.y)*(cell.y - this.y));
if(distance < min && this.mass > cell.mass + 10){
min = distance;
x = cells.indexOf(cell);
}else if(distance < min && this.mass < cell.mass + 10 && cell.cellCount == cells.size()){
x = -1;
}
}
}
return x;
}
此方法返回小区的索引与标准或-1。我的问题是:有什么办法可以避免这种OutofBoundsException?我已经尝试了多种方法,例如重复检查,但我仍然遇到同样的问题。
我建议你做适当的诊断工作,这样你就可以缩小成一个[mcve]。考虑到你不明白的行为(假设“忽略”if语句),我会试着首先解决这个问题。 –
我也建议在你的方法中调用'returnNearestCell' *一次*,并在整个方法中使用该结果。你为什么要多次打电话?我可能实际上会让它返回'Cell'(或者如果没有找到,则返回null),而不是索引,它会使你的代码变得更简单,我怀疑... –
它可以帮助知道它在什么位置抛出异常 –