找到:T [],必需:T []

时间:2019-04-12 17:17:03

标签: java algorithm sorting generics

我已经在通用方法中对排序算法 Quicksort 进行了编码,并带有以下参数:

  • 分区方法:(T[] array, int low, int high
  • 排序方法:(T[] array, int low, int high

但是,当我尝试在排序方法主体中进行递归时,对于array参数,它说

Wrong 1st arguement type. Found: 'T[]', required: 'T[]'

这是排序方法中的代码:

 if (low < high)
        { int pi = partition(array, low, high);
            Quicksort(array, low, pi-1);
            Quicksort(array, pi+1, high);
        }

这是分区方法中的代码:

T pivot = array[high];
        int i = (low-1); // index of smaller element
        for (int j=low; j<high; j++)
        {
            if (array[j].compareTo(pivot) <=0)
            {
                i++;
                // swap array[i] and array[j]
                T temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        // swap array[i+1] and array[high] (or pivot)
        T temp = array[i+1];
        array[i+1] = array[high];
        array[high] = temp;

        return i+1;

我很困惑如何尝试解决编译器错误。我尝试将其强制转换为(T)array,但它表示的是同一件事。在我看来,它希望参数以array[index]的形式出现,但这会使我的方法效率低下。

有什么建议可以解决此错误吗?


这是我的完整代码:

public class DD_ObjectBinarySearcher<T> {
    //comparison count for Binary search
    static int binarycount = 0;
    //comparison count for Sequential search
    static int seqcount = 0;
    //comparison totals for calculating averages
    static int stotal; static int btotal;

    /**
     *
     * @return total counts of Sequential Search
     */
    public static int getStotal() {
        return stotal;
    }

    /**
     *
     * @return total counts of Binary Search
     */
    public static int getBtotal() {
        return btotal;
    }

    /**
     * @param array array to be sorted
     * @param low starting index
     * @param high ending index
     * @return partition for quick sort
     */
    static  <T extends Comparable<T>> int partition(T[] array, int low, int high)
    {
        T pivot = array[high];
        int i = (low-1); // index of smaller element
        for (int j=low; j<high; j++)
        {
            if (array[j].compareTo(pivot) <=0)
            {
                i++;
                // swap array[i] and array[j]
                T temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }

        // swap array[i+1] and array[high] (or pivot)
        T temp = array[i+1];
        array[i+1] = array[high];
        array[high] = temp;

        return i+1;
    }

    /**
     * @param array array to be sorted
     * @param low starting index
     * @param high ending index
     */
    static <T> void Quicksort(T[] array, int low, int high)
    {
        if (low < high)
        { int pi = partition(array, low, high);
            Quicksort(array, low, pi-1);
            Quicksort(array, pi+1, high);
        }
    }

    /**
     * @param a array
     * @param b compared integer
     * @return flag
     */
    static <T extends Comparable<T>> boolean sequentialSearch(T[] a, T b){
        for (T i : a) {
            if (i==b){
                System.out.println("The number of comparisons for unsorted array: " + seqcount);
                stotal+=seqcount;
                return true;
            }
            seqcount++;
        }
        return false;
    }

    /**
     * @param a array
     * @param b compared integer
     * @return flag
     */
    static <T extends Comparable<T>> boolean binarySearch(T[] a, T b) {
        if (a.length == 0) return false;
        int low = 0;
        int high = a.length-1;

        while(low <= high ) {
            int middle = (low+high) /2;
            if (b.compareTo((T) a[middle]) > 0){
                low = middle +1;
            } else if (b.compareTo((T) a[middle]) < 0){
                high = middle -1;
            } else { // the element has been found
                System.out.println("The number of comparisons for sorted array: " + binarycount);
                btotal+=binarycount; //totals are used to calculate average in the main
                return true;
            }
            binarycount++;
        }
        return false;
    }

    /**
     *
     * @param array that will be printed
     */
    static void printArray(int[] array)
    {
        for (int value : array) System.out.print(value + " ");
        System.out.println();
    }

}

1 个答案:

答案 0 :(得分:6)

此处涉及的2种不同的通用方法均定义了类型参数TT中的Quicksort没有任何界限,因此根本不必是Comparable。但是,T中的partition必须具有Comparable<T>的上限。编译器错误无法说明全部原因,但由于T边界不匹配而显示错误。

使与T绑定的Quicksort相同。

static <T extends Comparable<T>> void Quicksort(T[] array, int low, int high)

通常出于灵活性的考虑,我们将这一想法又向前了一步,因为Consumer Super(来自PECS):

static <T extends Comparable<? super T>> void Quicksort(T[] array, int low, int high)

您应该将其添加到所有T的边界中,包括partition的边界和任何其他需要边界的边界。

因为您的所有方法都是static,所以甚至没有使用在类上定义的T。您可以安全地删除它。

class DD_ObjectBinarySearcher {