在填充网格时遇到了问题。每次我这样做的时候,我都会得到一个堆栈溢出错误。下面是我当前的代码:
public void removeSelfFromGrid() {
Grid<Actor> grid = getGrid();
int rows = grid.getNumRows();
int cols = grid.getNumCols();
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
Location loc = new Location(i, j);
laugh = new CKiller();
laugh.putSelfInGrid(grid, loc);
}
}
}如果需要,下面是构造函数
public CKiller()
{
Color c = null;
setColor(c);
getGrid();
getLocation();
location = new ArrayList<Location>();
location.add(getLocation());
setDirection(direction);
}这里是错误(部分错误,太大而不能全部发布。它只是重复了这两个语句):
java.lang.StackOverflowError
at info.gridworld.actor.Actor.putSelfInGrid(Actor.java:123)
at CKiller.removeSelfFromGrid(CKiller.java:120)它说这就是问题所在
laugh.putSelfInGrid(grid, loc);发布于 2014-05-02 07:32:20
请执行以下操作:
-Are在removeSelfFromGrid()方法调用之前定义了laugh?它之前没有指定的类型。
-Should变量location不是ArrayList吗?它可能是一个位置对象。
-Is已经定义了int direction吗?
-Why你在调用getGrid()和getLocation()吗?他们没有做任何有益的事情。
-Does CKiller从Actor类继承了putSelfInGrid()方法?
请包含CKiller类的完整代码以及包含removeSelfFromGrid()的主类。
发布于 2014-05-02 09:03:39
我认为你的问题在于你重写了removeSelfFromGrid()方法。您应该已经创建了一个新方法,比如fillGrid()。
当一个参与者调用putSelfInGrid()时,如果该Location中当前有另一个参与者,它将调用removeSelfFromGrid(),您可以覆盖它,以便用一个参与者填充Grid上的每个Location。如果网格上有任何其他参与者,它们将调用removeSelfFromGrid(),这将导致再次填充网格,依此类推。
只需修复removeSelfFromGrid()中的代码,将其放入一个新方法中并恢复以前的代码,您就应该很好了。
https://stackoverflow.com/questions/23418248
复制相似问题