简体   繁体   English

为什么不能使用generator创建通用的bubbleSort? (JAVA)

[英]Why can't I create a generic bubbleSort with generator? (Java)

I've been trying to learn Generics implementation of a sorting class, but I get this error: "Cannot make a static reference to the non-static method compare(T, T) from the type Comparator" (line 14). 我一直在尝试学习排序类的泛型实现,但是出现以下错误:“无法从类型Comparator静态引用非静态方法compare(T,T)”(第14行)。

Why do I get this message? 为什么会收到此消息? I haven't declared my sorting class static. 我尚未将排序类声明为静态。

I'm trying to make a sorting class which could be used for any type of class as long as it implements Comparator. 我正在尝试创建一个排序类,该类可以实现任何类型的类,只要它实现Comparator。

public class GenericBSort<T extends Comparator<T>> {

public GenericBSort(T[] arr){
     boolean needNextPass = true;
        for (int k = 1; k < arr.length && needNextPass; k++)
        { // Array may be sorted and next pass not needed
          needNextPass = false;
          for (int i = 0; i < arr.length - k; i++){  
              if (T.compare(arr[i], arr[i + 1])>0){ // Swap list[i] with list[i + 1]
                  T temp = arr[i];
                  arr[i] = arr[i + 1];
                  arr[i + 1] = temp;

                  needNextPass = true; // Next pass still needed
            }
          }
        }



}

} }

Thanks a lot! 非常感谢! (I know it's a newbie question but I really couldn't figure it out and need to understand this : ) (我知道这是一个新手问题,但我真的无法弄清楚,需要理解这一点:)

T represents a class, so in the line T代表一个类,所以在行中

if(T.compare(arr[i], arr[i + 1])>0)

you are doing a static method call. 您正在执行静态方法调用。

You probably need to change it for 您可能需要更改它

if(arr[i].compare(arr[i + 1])>0)

比较器对一个对象起作用,并且不是静态的-因此您应该从数组中获取两个值,在第一个上调用compare方法,然后将第二个作为参数传递。

I'm trying to make a sorting class which could be used for any type of class as long as it implements Comparator. 我正在尝试创建一个排序类,该类可以实现任何类型的类,只要它实现Comparator。

Well, that's an unusual path. 好吧,这是一条不寻常的道路。 Comparator normally is a separate class from the thing being sorted. Comparator通常是与要排序的事物分开的类。

It's sensible to make a sorting class which could be used for any type of class as long as it implements Comparable . 创建一个可以用于任何类型的类的排序类是明智的,只要它实现Comparable

You've also put much of the sorting logic in the constructor, which is a bit abnormal as well. 您还把很多排序逻辑放在构造函数中,这也有点不正常。 It's usually better to make a separate method that does the work. 通常最好制作一个单独的方法来完成工作。

A reasonable approach to doing this for the Comparable interface would be approximately as follows: Comparable接口执行此操作的合理方法大致如下:

public class ComparableSort<T extends Comparable<T>> {

    public ComparableSort() {
    }

    public void sort(T[] arr){
        boolean needNextPass = true;
        for (int k = 1; k < arr.length && needNextPass; k++)
        { // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arr.length - k; i++){
                if (arr[i].compareTo(arr[i + 1])>0){ // Swap list[i] with list[i + 1]
                    T temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;

                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }

    public static void main(String[] args) {
        Integer[] test = new Integer[]{5,3,2,4,1};
        ComparableSort<Integer> sorter = new ComparableSort<Integer>();
        sorter.sort(test);
        System.out.println(Arrays.toString(test));
    }
}

Alternatively, you can sensibly make a sorting class which can be used for any type of class at all with the provision of a Comparator for that class: 另外,您可以明智地创建一个排序类,该类可以用于任何类型的类,而为此类提供一个Comparator

public class ComparatorSort<T> {

    private final Comparator<T> comp;

    public ComparatorSort(Comparator<T> comp){
        this.comp = comp;
    }

    public void sort(T[] arr){
        boolean needNextPass = true;
        for (int k = 1; k < arr.length && needNextPass; k++)
        { // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arr.length - k; i++){
                if (comp.compare(arr[i], arr[i + 1])>0){ // Swap list[i] with list[i + 1]
                    T temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;

                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }

    public static void main(String[] args) {
        Integer[] test = new Integer[]{5,3,2,4,1};
        ComparatorSort<Integer> sorter = new ComparatorSort<Integer>(new Comparator<Integer>() {
            @Override
            public int compare(Integer integer, Integer integer2) {
                if (integer > integer2)
                    return 1;
                else if (integer < integer2)
                    return -1;
                return 0;
            }
        });
        sorter.sort(test);
        System.out.println(Arrays.toString(test));
    }
}

Of course bubble sort isn't a good sort, and there are better sorts built into Java libraries already, but this is a good learning exercise. 当然,冒泡排序不是一个很好的排序,而且Java库中已经内置了更好的排序,但这是一个很好的学习练习。

T.compare is a static reference. T.compare是静态引用。 Can't use that. 不能使用它。 You need to compare an instance with another instance. 您需要将一个实例与另一个实例进行比较。
Plus, doesn't compare take only a single argument? 另外,比较不只包含一个参数吗?

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

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