交换方法不起作用?

时间:2014-05-05 02:31:06

标签: java

public class QuickSort {
    private static <T extends Comparable<T>> int partition(T[] table, int first, int last) {

        T pivot = table[first];
        int up = first;
        int down = last;
        do {
            while ((up < last) && (pivot.compareTo(table[up]) >= 0)) {
                up++;
            }
            while (pivot.compareTo(table[down]) < 0) {
                down--;
            }
            if (up < down) {
                swap(table, up, down);
            } 
        }while (up < down);
        swap(table, first, down);
        return down;
    }

    public static void swap(int A[], int x, int y){
        int temp = A[x];
        A[x] = A[y];
        A[y] = temp;

    }
}

我认为它与int A[]有关,但我不确定如何修复它

1 个答案:

答案 0 :(得分:2)

table的类型为T[](隐式对象数组),您的交换方法需要int[]

将交换更改为:

public static void swap(Object A[], int x, int y){
    Object temp = A[x];
    A[x] = A[y];
    A[y] = temp;

}