的最低值我寫了下面的代碼得到一個double []
我的目標是獲得doble[] absOfSub
的lowst價值,但它提供了以下異常 在行compared= Double.compare(d2, d1);
Exception in thread "main" java.lang.StackOverflowError
爲什麼溢出以及如何解決它?
編輯
public class TestThe {
static double[] absOfSub = new double[5];
private static int index=0;
private static int compare(int currentIdx, int minIdx) {
if(index < absOfSub.length) {
if(absOfSub[currentIdx] < absOfSub[minIdx]) {
compare(currentIdx + 1, currentIdx);
} else {
compare(currentIdx + 1, minIdx);
}
}
return minIdx;
}
public static void main(String[] args) {
absOfSub[0]=1000;
absOfSub[1]=810;
absOfSub[2]=108;
absOfSub[3]=130;
absOfSub[4]=110;
double result;
int inndex= compare(0,1);
System.out.println(absOfSub[inndex]);
}
}
即使您的代碼可以修復,但通過Java中的遞歸實現'min'是不自然的。爲什麼不把它重新設計成一個簡單的迭代解決方案呢? – 2012-04-25 08:05:50
@MarkoTopolnik更多解釋?如何使它更簡單? – lonesome 2012-04-25 08:07:27