堆栈溢出异常
我在下面编写了代码。但它会打印这个异常,我真的不知道它是什么问题,请帮助我,谢谢
代码:
private void fillMinAverageTime() { //T(n) = O(n^3)
for (int i = list.size() - 2; i >= 0; i--) {
for (int j = i + 1; j < list.size(); j++) {
for (k = i; k <= j; k++) {
minOne = fillMinAverageTimeArray(i, j);
if (min == 0.0) {
min = minOne;
} else if (minOne < min) {
min = minOne;
}
}
min = 0.0;
minOne = 0.0;
minAverageTimeArray[i][j] = min + probability[i][j];
}
}
}
private double fillMinAverageTimeArray(int i, int j) {
if (i > j) {
return 0.0;
}
if (i == j) {
return minAverageTimeArray[i][i];
}
System.out.println(k+","+j+","+i);//EDITED
**return (fillMinAverageTimeArray(i, k - 1) + fillMinAverageTimeArray(k + 1, j));**//the line tat throws this exception
}
异常:
at OBST.MemoizedVersion.fillMinAverageTimeArray(MemoizedVersion.java:118)
at OBST.MemoizedVersion.fillMinAverageTimeArray(MemoizedVersion.java:118)
at OBST.MemoizedVersion.fillMinAverageTimeArray(MemoizedVersion.java:118)
编辑:它将打印:
2,3,2
3,3,2
1,2,1
2,2,1
1,3,1
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
I have written code below.but it will print this exception and i really don't know what is its problem,please help me thanks
CODE:
private void fillMinAverageTime() { //T(n) = O(n^3)
for (int i = list.size() - 2; i >= 0; i--) {
for (int j = i + 1; j < list.size(); j++) {
for (k = i; k <= j; k++) {
minOne = fillMinAverageTimeArray(i, j);
if (min == 0.0) {
min = minOne;
} else if (minOne < min) {
min = minOne;
}
}
min = 0.0;
minOne = 0.0;
minAverageTimeArray[i][j] = min + probability[i][j];
}
}
}
private double fillMinAverageTimeArray(int i, int j) {
if (i > j) {
return 0.0;
}
if (i == j) {
return minAverageTimeArray[i][i];
}
System.out.println(k+","+j+","+i);//EDITED
**return (fillMinAverageTimeArray(i, k - 1) + fillMinAverageTimeArray(k + 1, j));**//the line tat throws this exception
}
Exception:
at OBST.MemoizedVersion.fillMinAverageTimeArray(MemoizedVersion.java:118)
at OBST.MemoizedVersion.fillMinAverageTimeArray(MemoizedVersion.java:118)
at OBST.MemoizedVersion.fillMinAverageTimeArray(MemoizedVersion.java:118)
EDITED: it will print:
2,3,2
3,3,2
1,2,1
2,2,1
1,3,1
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
1,3,2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经编写了一个递归方法。您必须确保它将通过到达基本情况而终止,否则它可能会进入循环,直到发生堆栈溢出异常 - 这就是这里发生的情况。保证终止的最简单方法是始终确保每次调用都更接近基本情况。这里的基本情况是参数
i
和j
在某个时刻必须变得相等,因此您应该尝试在每一步中使它们至少彼此更接近。这是给出问题的行:
这只会重复使用相同的值调用您的方法。您确定您的意思不是
i + 1
和j - 1
而不是k + 1
和k - 1
代码>?You have written a recursive method. You have to be sure that it will terminate by reaching the base case, otherwise it could go into a loop until a stack overflow exception occurs - which is what is happening here. The easiest way to guarantee termination is to always ensure that you get closer to your base case on every call. Your base case here is that the arguments
i
andj
at some point must become equal, so you should try to bring them at least one closer to each other at every step.Here's the line that gives the problem:
This is just going to call your method with the same values repeatedly. Are you sure you didn't mean
i + 1
andj - 1
instead ofk + 1
andk - 1
?正如马克已经说过的,确保你的递归调用正确终止。
如果它仍然溢出,那么你的递归就太深了。
Hacky 解决方案,使用 JVM 的
-Xss
标志来 更改堆栈大小。正确的解决方案是使用迭代方法并推出自己的堆栈。
As Mark already said make sure that your recursive call terminates correctly.
If it still overflows then your recursion simply is to deep.
Hacky solution, use the JVM's
-Xss
flag to change the stack size.Correct solution, use an iterative approach and roll your own stack.