迷宫生成给我一个堆栈溢出错误

时间:2019-05-24 04:11:17

标签: java stack-overflow

在迷宫生成代码中,我遇到了堆栈溢出错误,为什么?我如何解决它?它仅在最后发生。如果我注释else语句中的if语句之一,则代码可以正常工作。为什么是这样? 我试图递归地生成一个迷宫,但不确定自己是否做对了

    public void generate(int row, int col) {

        rng = new Random();
        int move = rng.nextInt(4);

        if(hasUnvisited()==false) {
            System.out.println("e");
        }

        else if(move==0&&row+2<=19) {
            if (maze[row+2][col].getVisited()==false) {
                maze[row+1][col].remove();
            }
            maze[row][col].visit();
            generate(row+2,col);
        }
        else if(move==1&&row-2>=0) {
            if (maze[row-2][col].getVisited()==false) {
                maze[row-1][col].remove();
            }
            maze[row][col].visit();
            generate(row-2,col);
        }
        else if(move==2&&col+2<=19) {
            if (maze[row][col+2].getVisited()==false) {
                maze[row][col+1].remove();
            }
            maze[row][col].visit();
            generate(row,col+2);
        }
        else if(move==3&&col-2>=0) {
            if (maze[row][col-2].getVisited()==false) {
                maze[row][col-1].remove();
            }
            maze[row][col].visit();
            generate(row,col-2);
        }

        else {
            maze[row][col].visit();
            if (move==0) {
                generate(row-2,col);
            }

            else if (move==1) {
                generate(row+2,col);
            }

            else if (move==2) {
                generate(row,col-2);
            }

            else if (move==3) {
                generate(row,col+2);
            }
        }       
    }

Exception in thread "main" java.lang.StackOverflowError
    at java.util.Random.<init>(Unknown Source)
    at Maze.generate(Maze.java:41)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:94)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:94)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:88)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:88)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:88)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:94)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:94)
    at Maze.generate(Maze.java:88)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:88)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:67)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:74)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:60)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:53)
    at Maze.generate(Maze.java:67)

1 个答案:

答案 0 :(得分:0)

您的堆栈在告诉您两件事时非常冗长:

  • 实际上,堆栈溢出不是由遗忘的索引增量或类似原因引起的。
  • 从外观上看,代码没有陷入无限循环,因为堆栈帧终止于代码中的不同行。如果不同,则将有无限循环。它可能仍然是一个(似乎是随机性所隐藏),但可能性要小得多。

当前,每当您进行递归调用(在generate中调用generate)时,都会增加堆栈的大小。您将所有当前字段状态传递到堆栈帧中,并且在继续下一个generate的下一个调用时,VM必须记住这一点。因为这可能还会调用自身,依此类推,在某些时候,堆栈变得太大而无法存储在堆栈中。

我认为您有两种选择。启动VM时,要么增加最大堆栈大小 set JAVA_OPTS=%JAVA_OPTS% -Xms1024m -Xmx1024m 或(更好的选择)重新编写代码以使其迭代。这样,您就不会无限期地增加堆栈。那是什么意思:

您将不得不创建循环来通过显式设计访问堆栈的不同级别,而不是进行冗长的工作(对方法本身进行调用)。所有递归代码都可以(按照IT理论)转换为迭代代码,因此这应该是可能的。但是,您必须换个思路。