gpt4 book ai didi

java - ArrayOutOfBounds 异常合并排序

转载 作者:行者123 更新时间:2023-12-02 03:02:34 24 4
gpt4 key购买 nike

我正在研究合并排序,并且抛出了ArrayIndexOutOfBoundsException。我一直在梳理代码寻找错误,但找不到它。异常堆栈跟踪表明它来自第 77 行的合并函数,即 temp[current3++] = list2[current2++];。第 102 行是 merge(firstHalf, secondaryHalf, list);,第 93 行是 mergeSortRoutine(firstHalf);。非常感谢任何和所有的帮助。感谢您的时间。

下面是两个函数:

//this function will merge two arrays
private static void merge(Integer[] list1, Integer[] list2, Integer[] temp){
int current1 = 0; //current index in list1
int current2 = 0; //current index in list2
int current3 = 0; //current index in temp

while((current1 < list1.length) && (current2 < list2.length)){

if(list1[current1].intValue() < list2[current2].intValue()){
temp[current3++] = list1[current1++];
}
else{
temp[current3++] = list2[current2++];
}
}

while(current1 < list1.length){
temp[current3++] = list2[current2++];
}

while(current2 < list2.length) {
temp[current3++] = list2[current2++];
}
}

//merge sort function
private static void mergeSortRoutine(Integer[] list){

if(list.length > 1){

//merge sort the first half
Integer[] firstHalf = new Integer[(list.length)/2];
System.arraycopy(list, 0, firstHalf, 0, (list.length)/2);
mergeSortRoutine(firstHalf);

//merge sort the second half
int secondHalfLength = list.length - ((list.length)/2);
Integer[] secondHalf = new Integer[secondHalfLength];
System.arraycopy(list, (list.length)/2, secondHalf, 0, secondHalfLength);
mergeSortRoutine(secondHalf);

//merge firstHalf with secondHalf into list
merge(firstHalf, secondHalf, list);
}
}

下面是堆栈跟踪:

java.lang.ArrayIndexOutOfBoundsException: 1
at Merge.merge(Merge.java:77)
at Merge.mergeSortRoutine(Merge.java:102)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.mergeSortRoutine(Merge.java:93)
at Merge.main(Merge.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

最佳答案

正如可怕的袋熊所说,在你的最后 while 中循环检查 current2 然后递增它。

除此之外,您还要递增 current2在您的倒数第二个 while循环,while(current1 < list1.length) 。我认为您可能希望该声明为 temp[current3++] = list1[current1++] .

关于java - ArrayOutOfBounds 异常合并排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com