1

美好的一天,我正在实施八叉树。但是,当输入大小大于 2 时,我一直收到 StackOverflowError。我不知道为什么会收到此错误。

八叉树的实例化

octree = new Octree(3, new Point(0,0,0));

其中第一个参数是维度,第二个参数是根

八叉树人口的代码:

public void generateList(int num)
{
    for(int i = 0 ; i < num; i++)
        for (int j = 0 ; j < num; j++)
            for (int z = 0 ; z < num; z++)
                if (!(z == j && j == i))  {
                    octree.add_nodes(i,j,z);
                }
}

八叉树的 add_nodes 代码

public boolean add_nodes(double x, double y, double z) {

    boolean success = false;
    System.out.println(x+","+y+","+z);
    if (this.i < 8) {
        if (compare(x, y, z)) {
            if (root.childPoints == null) {
                System.out.println("Root is null" + x + " " + y + " " + z);
            }

            this.root.childPoints[i] = new Point(x, y, z);
            this.i++;
            success = true;
        }
    }

    else if (this.childNodes == null) {

        this.create_nodes_of_nodes(x, y, z);

        for (int j = 0; j < 8; j++) {
            double tempx = this.root.x - root.childPoints[j].x;
            double tempy = this.root.y - root.childPoints[j].y;
            double tempz = this.root.z - root.childPoints[j].z;

            int checker = compareValues(tempx, tempy, tempz);

            this.childNodes[checker].add_nodes(root.childPoints[j].x,
                    root.childPoints[j].y, root.childPoints[j].z);

        }
        root.childPoints = null;

        double tempx = this.root.x - x;
        double tempy = this.root.y - y;
        double tempz = this.root.z - z;
        int checker = compareValues(tempx, tempy, tempz);
        this.childNodes[checker].add_nodes(x, y, z);
        // this.i=0;
    }

    else {

        if (childNodes != null) {
            int checker = compareValues(x, y, z);
            childNodes[checker].add_nodes(x, y, z);

        }
    }

    return success;
}

错误:

Exception in thread "main" java.lang.StackOverflowError
at sun.misc.FloatingDecimal$BinaryToASCIIBuffer.appendTo(FloatingDecimal.java:307)
at sun.misc.FloatingDecimal.appendTo(FloatingDecimal.java:89)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:709)
at java.lang.StringBuilder.append(StringBuilder.java:226)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:73)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:97)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
at edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)
and an additional 400 lines of at 
edu.modesta.test.octree.Octree.add_nodes(Octree.java:107) error

希望有人能回答。谢谢!

4

1 回答 1

1

我不能 100% 确定,因为我看不到其余的代码,但看起来你在某些时候输入add_nodes()i >= 8. 从那时起,你的控制流说

if (this.childNodes == null),做一些事情然后打电话add_nodes()

else if (childNodes != null),做一些事情然后打电话add_nodes()

无论哪种方式,您都在调用add_nodes()ifi不小于 8,并且由于您从不 decrement i,因此每个后续调用也将调用add_nodes()

每次调用方法时,新方法的框架都会添加到堆栈中。StackOverflowException当堆栈最终变得太大时会发生A 。由于您假设调用add_nodes()了无限次,因此每次调用都会使堆栈增长一点,直到它变得太大,因此您的异常。

顺便说一句:我有点困惑为什么你提到childNodesasthis.childNodes和 just childNodesthis通常仅当您有两个具有不同范围但名称相同的变量时才需要,而您在此处没有。您可能还想重新使用this

于 2017-08-09T19:13:14.040 回答