美好的一天,我正在实施八月。但是,当输入大小大于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);
}
}
octree的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)
以及另外400行的edu.modesta.test.octree.Octree.add_nodes(Octree.java:107)错误
希望有人能回答。谢谢!
答案 0 :(得分:1)
我不是百分之百肯定,因为我看不到剩下的代码,但在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
通常只有在两个变量具有不同范围但名称相同的变量时才是必需的,这里没有。您可能也希望了解using this
。