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 使用对象、字符串和ArrayList进行插入排序_Java_Sorting_Arraylist_Insertion Sort - Fatal编程技术网

Java 使用对象、字符串和ArrayList进行插入排序

Java 使用对象、字符串和ArrayList进行插入排序,java,sorting,arraylist,insertion-sort,Java,Sorting,Arraylist,Insertion Sort,我的插入排序代码有问题 公共静态ArrayList InsertionSort ArrayList PlayerNames{ System.out.println("roster will now be sorted"); for (int i = 1; i < playersNames.size();i++){ int j = i - 1; Baller tempx = playersNames.get(i); Ba

我的插入排序代码有问题

公共静态ArrayList InsertionSort ArrayList PlayerNames{

    System.out.println("roster will now be sorted");

    for (int i = 1; i < playersNames.size();i++){

        int j = i - 1;
         Baller tempx = playersNames.get(i);
         Baller tempy = playersNames.get(j);
         while (j >= 0 &&  tempx.getName().compareToIgnoreCase(tempy.getName())< 0){
            playersNames.set(i, tempy);
            playersNames.set(j+1, tempx);
            j--;

         }


        System.out.println(playersNames);
    }

    return playersNames;
}

输出未正确输出。

我认为您需要对代码进行一些小更改,以解决您遇到的问题

让我们看一下代码的这一部分:

     int j = i - 1;
     Baller tempx = playersNames.get(i);
     Baller tempy = playersNames.get(j);
     while (j >= 0 &&  tempx.getName().compareToIgnoreCase(tempy.getName())< 0){
        playersNames.set(i, tempy);
        playersNames.set(j+1, tempx);
        j--;

     }
     int j = i - 1;
     while (j >= 0 &&
            playersNames.get(j).getName().compareToIgnoreCase(playersNames.get(j+1).getName().getName()) < 0){
        Baller temp = playersNames.get(j);
        playersNames.set(j, playersNames.get(j+1));
        playersNames.set(j+1, temp);
        j--;
     }