繁体   English   中英

在这种情况下如何正确使用 Comparables?

[英]How can I properly using Comparables in this situation?

我正在 Sedgewick 的算法教科书中做一个练习,练习是找到快速排序的比较次数。 下面是我的代码:

import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;
import java.util.Random;

public class quickSortCompareCheck {

    private static int numberOfCompares;


    private static Comparable[] generateArray(int n) {
        Random random = new Random();
        Comparable[] arr = new Comparable[n];
        for (int i = 0; i < n; i++) {
            arr[i] = random.nextInt();
        }
        return arr;
    }

    private static class QuickSort {

        private static void quickSort(Comparable[] arr) {
            StdRandom.shuffle(arr);
            quickSort(arr, 0, arr.length - 1);
        }

        private static void quickSort(Comparable[] arr, int lo, int hi) {
            if (lo >= hi) return;
            int partition = partition(arr, lo, hi);
            quickSort(arr, lo, partition - 1);
            quickSort(arr, partition + 1, hi);
        }

        private static int partition(Comparable[] arr, int lo, int hi) {
            Comparable pivot = arr[lo];
            int i = lo;
            int j = hi + 1;
            while (true) {
                numberOfCompares++;

                while (less(arr[++i], pivot)) {
                    if (i == hi) break;
                    numberOfCompares++;
                }

                numberOfCompares++;

                while(less(pivot, arr[--j])) {
                    if (j == lo) break;
                    numberOfCompares++;
                }

                if (i >= j) break;
                exchange(arr, i, j);
            }
            exchange(arr, lo, j);
            return j;
        }

        private static boolean less(Comparable v, Comparable w) {
            return v.compareTo(w) < 0;
        }

        private static void exchange(Comparable[] arr, int i, int j) {
            Comparable temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }


    }


    public static void main(String[] args) {
        int[] sizes = {100,1000,10000};
        for (int i = 0; i < sizes.length; i++) {
            int size = sizes[i];
            double exp = 2 * size * Math.log(size);
            Comparable[] arr = generateArray(sizes[i]);
            QuickSort.quickSort(arr);
            StdOut.println("Expected compares is: " + exp);
            StdOut.println("Actual compares is: " + numberOfCompares);
            numberOfCompares = 0;
        }
        
    }
}

我收到以下错误:

quickSortCompareCheck.java:10: warning: [rawtypes] found raw type: Comparable
    private static Comparable[] generateArray(int n) {
                   ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:12: warning: [rawtypes] found raw type: Comparable
        Comparable[] arr = new Comparable[n];
        ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:12: warning: [rawtypes] found raw type: Comparable
        Comparable[] arr = new Comparable[n];
                               ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:21: warning: [rawtypes] found raw type: Comparable
        private static void quickSort(Comparable[] arr) {
                                      ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:26: warning: [rawtypes] found raw type: Comparable
        private static void quickSort(Comparable[] arr, int lo, int hi) {
                                      ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:33: warning: [rawtypes] found raw type: Comparable
        private static int partition(Comparable[] arr, int lo, int hi) {
                                     ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:34: warning: [rawtypes] found raw type: Comparable
            Comparable pivot = arr[lo];
            ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:59: warning: [rawtypes] found raw type: Comparable
        private static boolean less(Comparable v, Comparable w) {
                                    ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:59: warning: [rawtypes] found raw type: Comparable
        private static boolean less(Comparable v, Comparable w) {
                                                  ^
  missing type arguments for generic class Comparable<T>
  where T is a type-variable:
    T extends Object declared in interface Comparable
quickSortCompareCheck.java:60: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type Comparable
            return v.compareTo(w) < 0;
                              ^
  where T is a type-variable:
    T extends Object declared in interface Comparable
10 warnings

I'm pretty new to Java and I'm not sure how to fix the code, I got it to work using the Integer class instead of Comparable since Integer implements Comparable, but I'd like to get my code working as it is. 有一个解决方案( https://github.com/reneargento/algorithms-sedgewick-wayne/blob/master/src/chapter2/section3/Exercise6.java )来生成与我相似的随机数组的人. 有人可以教我如何解决这个问题吗?

有大量关于 generics 的信息,但并不总是那么容易掌握,所以我决定在这里做一个“小案例研究”。

查看Java 文档以获得 Comparable<T> 摘自它的最开始:

接口可比<T>

类型参数:
T - 此 object 可与之比较的对象类型

警告是因为您没有为您的可比对象声明类型T。 为任何泛型 class 声明类型的目的是它允许编译器在编译时检查泛型类型是否正确使用。

如果您不提供类型,则无法进行此检查,并且它会在您拥有任何东西的每个地方发出这些警告,而无需提供类型,因此是原始类型 这意味着不能保证会出现一些错误的实例 - 在你的情况下 - Comparable被使用。

在您的情况下,类型将是Integer 就像Comparable<Integer>一样。 但是,它需要对您的代码进行一些重构,并且没有意义,因为Integer实现了Comparable<Integer>所以它本身就是一个Comparable<Integer>

作为结论,您可以 - 例如 - 只需将Comparator的出现替换为Integer

也可以使整个东西通用,以便它可以用于String或任何其他可比较的,但如前所述,它需要一些重构,并且超出了这个问题和答案的 scope。

免责声明:我没有以任何方式测试您的代码,因此请对其进行测试。

(顺便说一句:检查 Java 文档还表明 StdRandom 和 StdOut 接受整数)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM