簡體   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