Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.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 - Fatal编程技术网

Java 有人能指出合并函数中的错误吗

Java 有人能指出合并函数中的错误吗,java,Java,运行代码时,会显示IndexOutBoundException 我不知道为什么会发生这种情况,这与merge函数有关 public class MergeSort { public static <T extends Comparable<T>> void sort(List<T> lst) { System.out.println(lst.size()); mergeSort(lst, 0, lst.size()

运行代码时,会显示
IndexOutBoundException
我不知道为什么会发生这种情况,这与merge函数有关

 public class MergeSort {

    public static <T extends Comparable<T>> void sort(List<T> lst) {
        System.out.println(lst.size());

        mergeSort(lst, 0, lst.size() - 1);

    }

    private static <T extends Comparable<T>> void mergeSort(List<T> l, int low,int high) {

        if (low < high) {
            int mid = low +( (high-low) / 2);
            mergeSort(l, low, mid);
            mergeSort(l, mid + 1, high);
            merge(l, low, high, mid);

        }
    }

    private static <T extends Comparable<T>> void merge(List<T> lst, int low,int high, int mid) {
        List<T> temp = new ArrayList<T>();

        for (int i = low; i <= high; i++) {
            System.out.println(lst.get(i));
            temp.add(i,lst.get(i));
        }

        System.out.println();

        int i = low;
        int j = mid + 1;
        int k = low;
        while (i <= mid && j <= high) {
            if ((temp.get(i)).compareTo(temp.get(j)) <= 0) {
                lst.set(k, temp.get(i));
                i++;
            } else {
                lst.set(k, temp.get(j));
                j++;
            }
            k++;
        }
        while (i <= mid || j <= high) {
            if (i <= mid) {
                lst.set(k, temp.get(i));
                k++;
                i++;
            } else if (j <= high) {
                lst.set(k, temp.get(j));
                k++;
                j++;
            }
        }
        for(T e:lst ){
            System.out.print(e+ " ");
        }
        System.out.println();
    }

IndexOutOfBoundsException的意思很简单,就是您试图访问一个不存在的元素。在你得到它之前,试着检查它是否存在


错误告诉您,即使列表大小为零,您仍试图访问索引3处的元素。

您的异常被抛出到行中

temp.add(i,lst.get(i));
这是因为您试图在不存在的索引中设置列表的值。变量
temp
指向空列表;因此,对大于0的索引的任何赋值都将抛出
IndexOutOfBoundsException
。这记录在界面中:

void add(int索引,E元素)

在此列表中的指定位置插入指定元素(可选操作)。将当前位于该位置的元素(如果有)和任何后续元素向右移动(将一个元素添加到其索引中)

[……]

抛出:

[……]

IndexOutOfBoundsException-如果索引超出范围
(索引<0 | | index>size())

虽然不是最有效的,但解决问题的简单方法是在创建临时列表后立即使用空值初始化临时列表,然后
set
而不是
add
相关值

private static <T extends Comparable<T>> void merge(List<T> lst, int low,
    int high, int mid) {

    List<T> temp = new ArrayList<T>();

    // Fill temp list with empty values
    for(int i=0; i<lst.size(); i++){
        temp.add(null);
    }

    // Set the values we are going to work on.
    for (int i = low; i <= high; i++) {
        System.out.println(lst.get(i));
        temp.set(i,lst.get(i));
    }

    ...
}
private静态无效合并(列表lst,整型低,
整数高,整数中){
List temp=new ArrayList();
//用空值填充临时列表
对于(int i=0;i
private static <T extends Comparable<T>> void merge(List<T> lst, int low,
    int high, int mid) {

    List<T> temp = new ArrayList<T>();

    // Fill temp list with empty values
    for(int i=0; i<lst.size(); i++){
        temp.add(null);
    }

    // Set the values we are going to work on.
    for (int i = low; i <= high; i++) {
        System.out.println(lst.get(i));
        temp.set(i,lst.get(i));
    }

    ...
}