Java 递归函数上的StackOverflow
我有一个函数,这个函数可以产生笛卡尔平面上的所有坐标,我可以从原点以n步到达: 原点为“位置”,步数为“强度”,为整数1-10。然而,我不断得到一个stackoverflow错误。每次我调用它,然后调用ArrayList位置上的clear。想法 更新代码:Java 递归函数上的StackOverflow,java,recursion,stack-overflow,Java,Recursion,Stack Overflow,我有一个函数,这个函数可以产生笛卡尔平面上的所有坐标,我可以从原点以n步到达: 原点为“位置”,步数为“强度”,为整数1-10。然而,我不断得到一个stackoverflow错误。每次我调用它,然后调用ArrayList位置上的clear。想法 更新代码: // Returns all positions reachable in 'strength' steps public ArrayList<Int2D> findEscapeSpace(Int2D location,
// Returns all positions reachable in 'strength' steps
public ArrayList<Int2D> findEscapeSpace(Int2D location, Field f) {
// Are we still within the given radius?
if((Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) < strength) {
System.out.println("Starting on " + location);
// If this position is not contained already, and if it doesn't contain a wall
if(!positions.contains(location) && f.wallField.getObjectsAtLocation(location) == null) {
positions.add(location);
System.out.println("added " + location);
}
// Getting neighboring positions
ArrayList<Int2D> neigh = findNeighPos(location, f);
for(Int2D pos : neigh) {
System.out.println("looking into " + pos + " at depth " + (Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) + " and strength " + strength);
if(!positions.contains(pos))
findEscapeSpace(pos, f);
}
}
System.out.println(positions.size());
return positions;
}
//返回在“强度”步骤中可到达的所有位置
公共ArrayList FinDescape空间(Int2D位置,字段f){
//我们还在给定的半径范围内吗?
if((Math.abs(location.getX()-this.location.getX())+Math.abs(location.getY()-this.location.getY())
旧代码
public ArrayList<Int2D> positions = new ArrayList<Int2D>();
// Returns all positions reachable in 'strength' steps
public ArrayList<Int2D> findEscapeSpace(Int2D location, Field f) {
// Are we still within the given radius?
if((Math.abs(location.getX() - this.location.getX()) + Math.abs(location.getY() - this.location.getY())) < strength) {
// If this position is not contained already, and if it doesn't contain a wall
if(!positions.contains(location) && f.wallField.getObjectsAtLocation(location) == null)
positions.add(location);
// Getting neighboring positions
ArrayList<Int2D> neigh = findNeighPos(location, f);
for(Int2D pos : neigh) {
findEscapeSpace(pos, f);
}
}
return positions;
}
public ArrayList<Int2D> findNeighPos(Int2D currentP, Field f) {
ArrayList neighPositions = new ArrayList<Int2D>();
int cx = currentP.getX();
int cy = currentP.getY();
int maxY = f.HEIGHT-1;
int maxX = f.WIDTH-1;
// A few checks to make sure we're not going off tack (literally)
if(cx > 0 && cy < maxY)
neighPositions.add(new Int2D(cx-1, cy+1));
if(cy < maxY)
neighPositions.add(new Int2D(cx, cy+1));
if(cx < maxX && cy < maxY)
neighPositions.add(new Int2D(cx+1, cy+1));
if(cx > 0)
neighPositions.add(new Int2D(cx-1, cy));
if(cx < maxX)
neighPositions.add(new Int2D(cx+1, cy));
if(cx > 0 && cy > 0)
neighPositions.add(new Int2D(cx-1, cy-1));
if(cy > 0)
neighPositions.add(new Int2D(cx, cy-1));
if(cx < maxX && cy > 0)
neighPositions.add(new Int2D(cx+1, cy-1));
return neighPositions;
}
public ArrayList positions=new ArrayList();
//返回“强度”步骤中可到达的所有位置
公共ArrayList FinDescape空间(Int2D位置,字段f){
//我们还在给定的半径范围内吗?
if((Math.abs(location.getX()-this.location.getX())+Math.abs(location.getY()-this.location.getY())0&&cy0)
新增(新Int2D(cx-1,cy));
if(cx0&&cy>0)
新增(新Int2D(cx-1,cy-1));
如果(cy>0)
新增(新Int2D(cx,cy-1));
如果(cx0)
新增(新Int2D(cx+1,cy-1));
返回邻近位置;
}
您的递归似乎没有终止条件。看起来您可能希望将strength
作为参数传递给findEscapeSpace()
,当该方法递归时,它传递的值小于传递给它的值
除此之外,您的算法看起来相当低效,因为它可能会多次生成和测试许多可到达的单元,而且,检查每个单元是否已被找到的成本相对较高。但这是要克服的下一个问题。您的递归似乎没有终止条件。看起来您可能希望将
strength
作为参数传递给findEscapeSpace()
,当该方法递归时,它传递的值小于传递给它的值
除此之外,您的算法看起来相当低效,因为它可能会多次生成和测试许多可到达的单元,而且,检查每个单元是否已被找到的成本相对较高。但这是要克服的下一个问题。您的递归似乎没有终止条件。看起来您可能希望将
strength
作为参数传递给findEscapeSpace()
,当该方法递归时,它传递的值小于传递给它的值
除此之外,您的算法看起来相当低效,因为它可能会多次生成和测试许多可到达的单元,而且,检查每个单元是否已被找到的成本相对较高。但这是要克服的下一个问题。您的递归似乎没有终止条件。看起来您可能希望将
strength
作为参数传递给findEscapeSpace()
,当该方法递归时,它传递的值小于传递给它的值
除此之外,您的算法看起来相当低效,因为它可能会多次生成和测试许多可到达的单元,而且,检查每个单元是否已被找到的成本相对较高。但这是下一个需要克服的问题。(Math.abs(location.getX()-this.location.getX())+Math.abs(location.getY()-this.location.getY())