如果语句忽略 return 方法,还有其他解决方案吗?
If statement ignores return method, other solutions?
我有一个名为 Cells 的 class,其中有一个运行这段代码的更新方法:
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;
}
此方法returns 具有条件或-1 的单元格的索引。我的问题是:有什么办法可以避免这种 OutofBoundsException 吗?我已经尝试了多种方法,比如双重检查,但我仍然遇到同样的问题。
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);
在这里,您正在改变单元格,然后再次调用 returnNearestCell()
。由于该方法现在使用更改后的参数运行,因此 return 值可能不同。最重要的是,您沿着 x
坐标移动单元格,然后在下一次调用 returnNearestCell()
.
时计算时它位于不同的位置
您可能需要查找非原子更新 和并发修改 以了解有关此主题的更多信息。
Is there any way to store an object into a variable and access it through that variable?
是的,它是您问题的解决方案:
if (goalReached) {
// retrieve nearest cell once before modification
final int nearestCellIndex = returnNearestCell();
if (nearestCellIndex > -1 && isTarget) {
// save cell.
final Cell nearestCell = cells.get(nearestCellIndex);
if (this.checkCollide(nearestCell.x, nearestCell.y, nearestCell.mass)) {
// remainder of your code
}
}
}
请注意,最好直接拥有 returnNearestCell()
return Optional<Cell>
或至少一个 Cell
对象。 checkCollide()
也是如此,它可以简单地将 Cell
对象作为参数。
我有一个名为 Cells 的 class,其中有一个运行这段代码的更新方法:
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;
}
此方法returns 具有条件或-1 的单元格的索引。我的问题是:有什么办法可以避免这种 OutofBoundsException 吗?我已经尝试了多种方法,比如双重检查,但我仍然遇到同样的问题。
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);
在这里,您正在改变单元格,然后再次调用 returnNearestCell()
。由于该方法现在使用更改后的参数运行,因此 return 值可能不同。最重要的是,您沿着 x
坐标移动单元格,然后在下一次调用 returnNearestCell()
.
您可能需要查找非原子更新 和并发修改 以了解有关此主题的更多信息。
Is there any way to store an object into a variable and access it through that variable?
是的,它是您问题的解决方案:
if (goalReached) {
// retrieve nearest cell once before modification
final int nearestCellIndex = returnNearestCell();
if (nearestCellIndex > -1 && isTarget) {
// save cell.
final Cell nearestCell = cells.get(nearestCellIndex);
if (this.checkCollide(nearestCell.x, nearestCell.y, nearestCell.mass)) {
// remainder of your code
}
}
}
请注意,最好直接拥有 returnNearestCell()
return Optional<Cell>
或至少一个 Cell
对象。 checkCollide()
也是如此,它可以简单地将 Cell
对象作为参数。