Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
合并排序索引越界java_Java_Sorting_Mergesort - Fatal编程技术网

合并排序索引越界java

合并排序索引越界java,java,sorting,mergesort,Java,Sorting,Mergesort,嘿,我正在研究一种合并排序方法,它不断地获取索引越界错误。我不知道为什么会发生这种事,也不知道发生在哪里。我试着打印出索引,看看递归中是否有错误,但我不认为有,这是非常直接的 public ArrayList<String> mergeSort(ArrayList<String> words,int first, int last){ if (first < last){ int mid = (first+ last)/2;

嘿,我正在研究一种合并排序方法,它不断地获取索引越界错误。我不知道为什么会发生这种事,也不知道发生在哪里。我试着打印出索引,看看递归中是否有错误,但我不认为有,这是非常直接的

public ArrayList<String> mergeSort(ArrayList<String> words,int first, int last){

    if (first < last){
        int mid = (first+ last)/2;
        mergeSort(words,first,mid);
        mergeSort(words,mid+1,last);
        merge(words, first, mid, last);
    }
    return words;

}
public ArrayList<String> merge(ArrayList<String> words, int first, int mid, int last){
    int first1 = first;
    int last1 = mid;
    int first2 = mid+1;
    int last2 = last;
    int total = first1;
    ArrayList<String> temp = new ArrayList<String>();
    while ((first1<=last) && (first2 <= last2)){
        if (words.get(first1).compareTo(words.get(first2))<=0){
            temp.add(total,words.get(first1));
            first1++;
        }
        else{
            temp.add(total,words.get(first2));
            first2++;
        }
        total++;
    }
        while(first1 <= words.size()){
            temp.add(total,words.get(first1));// exception occurs here
        first1++;
        total++;
        }
        while (first2 <= last2){
            temp.add(total,words.get(first2));
            first2++;
            total++;
        }
    for (total = first; total <= last; ++total){
        words.set(total,temp.get(total));
    }
    System.out.println(words);
    return words;
}
publicArrayList合并排序(ArrayList单词,int-first,int-last){
如果(第一次<最后一次){
int mid=(第一个+最后一个)/2;
合并排序(单词,第一个,中间);
合并排序(单词,中间+1,最后);
合并(单词,第一,中间,最后);
}
返回单词;
}
公共ArrayList合并(ArrayList单词,int-first,int-mid,int-last){
int first1=第一;
int last1=mid;
int first2=中间+1;
int last2=最后;
int-total=first1;
ArrayList temp=新的ArrayList();

而((first1您的问题在于这样的行:

temp.set(total,words.get(first1));

您在一个没有元素的
ArrayList
上执行此操作。当您调用
.set()
时,必须向它传递数组中已存在的元素的索引(即
total您的问题是在空临时列表上使用
set()
set()
替换数组中的元素,但数组为空:没有要替换的元素

您需要做的是将
add()
添加到临时列表中,而不是使用
set()
。元素将按顺序添加,因此顺序正确,您不需要使用
total
变量

现在,当你替换回words中的元素时,情况会有所不同。temp列表中的元素将从
0
索引到
(last-first)
,你想用words替换
first
last
的元素。为此,我认为以下循环将起作用:

for (String word : temp) {
    words.set(first++, word);
}
注意:由于您事先知道温度的最终大小(即
最后一个-第一个+1
),因此应使用以下方法预先分配温度:

ArrayList<String> temp = new ArrayList<String>(last - first + 1);
ArrayList temp=新的ArrayList(last-first+1);

你为什么不看一下异常?它告诉你在哪里…请在发生错误的地方标记一行告诉我第50行我们怎么知道你机器上的第50行是什么?你必须解释你发布的哪些行是第50行!我已经将异常行的注释改为注释,因为
->
没有太大帮助在这一点上,它使Java无效。我将集合更改为“添加”,但仍然越界,但在退出第一个while循环后,现在是第61行。@user3400512是的,我已经指出了在该行出现错误的原因,但您需要重新编写代码以获得完整的解决方案。正如我所说的,您需要两个临时列表,一个对于数组的每一半,这样您就可以将它们合并回。我实现了循环,但仍然在同一位置获得了索引超出绑定异常。我看到您已编辑程序,将set(index,object)替换为add(index,object)。但是,您使用了错误版本的add()。您需要使用add(object)在列表末尾添加,而不是作为不存在的特定索引添加,因为您从一个空的临时列表开始,并且您还可以删除“total”变量。它不需要作为全部