Java Arraylist中的选择排序

Java Arraylist中的选择排序,java,sorting,arraylist,iteration,Java,Sorting,Arraylist,Iteration,我需要调试代码的帮助。它一直在犯错误,我不知道在哪里。以下是我的选择排序方法: private void selectionSort() { int best = 0; int j = 0; SortableRobot bestSoFar = botList.get(0); for(int i = 0;i<botList.size();i++) { int[] temp = botList.get(j)

我需要调试代码的帮助。它一直在犯错误,我不知道在哪里。以下是我的选择排序方法:

private void selectionSort() {
        int best = 0;
        int j = 0;
        SortableRobot bestSoFar = botList.get(0);
        for(int i = 0;i<botList.size();i++) {
            int[] temp = botList.get(j).getLocation();
            for(int x = j;x<botList.size();x++) {
                if(botList.get(j).compareTo(botList.get(x)) < 0) {
                    // botList.get(j).moveToLocation(botList.get(x).getLocation());
                    // botList.get(x).moveToLocation(temp);
                    bestSoFar = botList.get(x);
                    best = x;
                }
            }
            SortableRobot tempbot = botList.get(j);
            botList.set(best,tempbot);
            botList.set(j, bestSoFar);
            j++;
        }
    }

问题是变量bestSoFar必须在每次迭代开始时设置

此编辑使其在我的测试中起作用:

import java.util.ArrayList;
import java.util.List;

public class Test {

    private List<SortableRobot> botList;

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        botList = new ArrayList<SortableRobot>();
        botList.add(new SortableRobot(5));
        botList.add(new SortableRobot(3));
        botList.add(new SortableRobot(4));
        botList.add(new SortableRobot(1));
        botList.add(new SortableRobot(2));

        System.out.println("before sort: " + botList);
        selectionSort();
        System.out.println("after sort:  " + botList);
    }

    private void selectionSort() {
        int best = 0;
        int j = 0;
        SortableRobot bestSoFar = botList.get(0);
        for (int i = 0; i < botList.size(); i++) {
            bestSoFar = botList.get(j);// EDITED HERE the best bot so far has to be set to the current bot in the beginning of every iteration
            // int[] temp = botList.get(j).getLocation();
            for (int x = j; x < botList.size(); x++) {
                if (botList.get(j).compareTo(botList.get(x)) < 0) {
                    // botList.get(j).moveToLocation(botList.get(x).getLocation());
                    // botList.get(x).moveToLocation(temp);
                    bestSoFar = botList.get(x);
                    best = x;
                }
            }
            SortableRobot tempbot = botList.get(j);
            botList.set(best, tempbot);
            botList.set(j, bestSoFar);
            j++;
        }
    }

    private class SortableRobot implements Comparable<SortableRobot> {

        private int sortIndex;

        public SortableRobot(int sortIndex) {
            this.sortIndex = sortIndex;
        }

        public String toString() {
            return "SortableRobot[" + sortIndex + "]";
        }

        public int compareTo(SortableRobot o) {
            return Integer.compare(sortIndex, o.sortIndex);
        }
    }
}
/* *选择1个元素索引,并与上一个索引之前的所有元素进行比较。如果下一个值大于比较值,则交换这两个值 *在得到最后一个索引之前,也要这样做。 */

before sort: [SortableRobot[5], SortableRobot[3], SortableRobot[4], SortableRobot[1], SortableRobot[2]]
after sort:  [SortableRobot[5], SortableRobot[4], SortableRobot[3], SortableRobot[2], SortableRobot[1]]
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SortingSelection {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(Arrays.asList(40, 10, -30, 45, 39, 32));

        for (int i = 0; i < list.size(); i++) {
            int selectionIndex = i;

            for (int j = selectionIndex + 1; j < list.size(); j++) {
                if (list.get(selectionIndex) > list.get(j)) {
                    int temp = list.get(selectionIndex);
                    list.set(selectionIndex, list.get(j));
                    list.set(j, temp);

                }
            }
            System.out.println(list);
        }
    }

}