你好,我正在执行一个八叉树。然而,当输入大小大于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希望有人能回答。谢谢!
发布于 2017-08-09 19:13:14
我不能百分之百肯定,因为我看不到其余的代码,但在某个时候,您在add_nodes()中输入了i >= 8。从那以后,你的控制流说
if (this.childNodes == null),做些事情然后给add_nodes()打电话,
else if (childNodes != null),做些事情然后给add_nodes()打电话。
无论哪种方式,如果add_nodes()不小于8,则调用i,而且由于从未减少i,随后的每个调用也将调用add_nodes()。
每次调用方法时,新方法的框架都会被添加到堆栈中。当堆栈最终变得太大时,就会发生StackOverflowException。由于您假设调用add_nodes()无限次,所以每次调用都会稍微增加堆栈,直到堆栈变得太大,从而导致异常。
顺便提一下:我有点搞不懂为什么您将childNodes称为this.childNodes,也就是childNodes。只有当您有两个具有不同作用域但名称相同的变量时,this才是必需的,而这里没有这个名称。您可能也想浏览一下this。
https://stackoverflow.com/questions/45598773
复制相似问题