以下错误显示: 在括号.check.printcombination(check.java:51)
at parenthesis.check.printcombination(check.java:58) 我的第51行和第56行在下面的代码
中被注释package parenthesis;
import java.util.*;
public class check {
public static void main(String args[]) {
int arr[] = { 12, 13, 14, 15, 16, 17, 18, 19 };
function(arr, 0, 7);
}
public static void function(int arr[], int start, int end) {
int mid;
mid = (start + end) / 2;
printcombination(arr, start, end, mid);
}
public static void printcombination(int arr[], int start, int end, int mid) {
System.out.print(arr[mid] + " "); //// error
if ((start == mid) && (mid == end)) {
return;
}
printcombination(arr, start, mid - 1, (start + mid - 1) / 2); //// error
printcombination(arr, mid + 1, end, (mid + 1 + end) / 2);
}
}
答案 0 :(得分:1)
第34行的递归错误(“ printcombination(arr,start,mid-1,(start + mid-1)/ 2); ////错误”),此处引发了StackOverflowError错误。 “ printcombination”方法的递归流程为:
开始= 0,中间= 3,结束= 7,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
开始= 0,中间= 1,结束= 2,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
开始= 0,中间= 0,结束= 0,返回;
开始= 0,中间= 1,结束= 2,调用“ printcombination(arr,mid + 1,end,(mid + 1 + end)/ 2);
开始= 0,中= 3,结束= 7,调用printcombination(arr,mid + 1,end,(mid + 1 + end)/ 2);
开始= 4,中间= 5,结束= 7,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
开始= 4,中间= 4,结束= 4,返回;
开始= 4,中间= 5,结束= 7,调用“ printcombination(arr,mid + 1,end,(mid + 1 + end)/ 2)”;
开始= 6,中间= 6,结束= 7,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
开始= 6,中间= 5,结束= 5,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
开始= 6,中间= 5,结束= 4,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
开始= 6,中间= 5,结束= 4,调用“ printcombination(arr,start,mid-1,(start + mid-1)/ 2)”;
...
答案 1 :(得分:0)
尝试或代替和:if((start==mid)||(mid==end))