java - Msort 错误,通用

标签 java

我在使用 msort 方法时遇到问题。我没有任何错误,但是当我编译时, 我在以下行中收到错误:

if (((Comparable)arr[midpt-1]).compareTo(arr[midpt]) <= 0)

错误提示:

The method CompareTo(Object) belongs to the raw type Comparable. References to generic type comparable should be parametrized.

有什么帮助吗?

private static void msort(Object[] arr, Object[] tempArr, int first, int last)
{
    // if the sublist has more than 1 element continue
    if (first + 1 < last)
    {
        // for sublists of size 2 or more, call msort()
        // for the left and right sublists and then
        // merge the sorted sublists using merge()
        int midpt = (last + first) / 2;

        msort(arr, tempArr,first, midpt);
        msort(arr, tempArr, midpt, last);

        // if list is already sorted, just copy from src to
        // dest. this is an optimization that results in faster
        // sorts for nearly ordered lists.
        if (((Comparable)arr[midpt-1]).compareTo(arr[midpt]) <= 0)
            return;

        // the elements in the ranges [first,mid) and [mid,last) are
        // ordered. merge the ordered sublists into
        // an ordered sequence in the range [first,last) using
        // the temporary array
        int indexA, indexB, indexC;

        // set indexA to scan sublist A (index range [first,mid)
        // and indexB to scan sublist B (index range [mid, last)
        indexA = first;
        indexB = midpt;
        indexC = first;

        // while both sublists are not exhausted, compare arr[indexA] and
        // arr[indexB]; copy the smaller to tempArr
        while (indexA < midpt && indexB < last)
        {
            if (((Comparable)arr[indexA]).compareTo(arr[indexB]) < 0)
            {
                tempArr[indexC] = arr[indexA]; // copy element to tempArr
                indexA++;                      // increment indexA
            }
            else
            {
                tempArr[indexC] = arr[indexB]; // copy element to tempArr
                indexB++;                      // increment indexB
            }
            // increment indexC
            indexC++;
        }

        // copy the tail of the sublist that is not exhausted
        while (indexA < midpt)
        {
            tempArr[indexC] = arr[indexA]; // copy element to tempArr
            indexA++;
            indexC++;
        }

        while (indexB < last)
        {
            tempArr[indexC] = arr[indexB]; // copy element to tempArr
            indexB++;
            indexC++;
        }

        // copy elements from temporary array to original array
        for (int i = first; i < last; i++)
        arr[i] = tempArr[i];
    }
}

最佳答案

如果不使用@Suppresswarnings,您就无法消除所有警告,因为您正在执行未经检查的转换为Comparable

由于您在内部要求您的 Object 可以转换为 Comparable,因此在方法声明中声明这一点更有意义:

private static <T extends Comparable<T>> void msort(T[] arr, T[] tempArr, int first, int last) {

然后你的比较行就变成:

if (arr[midpt - 1].compareTo(arr[midpt]) <= 0) {

关于java - Msort 错误,通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16871856/

相关文章:

java - 解析 Hibernate Man.hbm.xml 时出错

java - 如何转换 PrimeFaces p :dataTable to standard h:dataTable (without skin) and then print it

java - 延迟 Kafka Streams 消费

java - 将 View 代码放在 Controller 中是不好的做法吗?

java - 循环遍历结果集以按组生成平均值

Java File.exists() 与 File.isFile()

java - 当连接为 http 时 request.isSecure() 返回 true

java - Spring异常错误

java - 如何使用 Java 的 Comparable 来比较树中的通用对象?

java - Struts 2中使用ModelDriven接口(interface)时无法解析select标签中的list属性