의 가장 낮은 값을 얻을 나는 다음과 같은 코드더블 []
을 썼다내 목표는 doble[] absOfSub
의 lowst 값을 얻는 것입니다하지만 라인 compared= Double.compare(d2, d1);
Exception in thread "main" java.lang.StackOverflowError
왜 오버 플로우에서 다음과 같은 예외 을 제공합니다 그것을 고치는 방법?
당신은 당신의 방법 내부 index
의 값을 변경하지 않는
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 '을 구현하는 것은 부 자연스러운 일입니다. 단순한 반복 솔루션으로 재 작업하지 않는 이유는 무엇입니까? –
@MarkoTopolnik 더 많은 설명? 어떻게하면 더 간단하게 만들 수 있을까요? – lonesome