Java 在构造函数中使用自定义的比较器

Java 在构造函数中使用自定义的比较器,java,sorting,comparator,Java,Sorting,Comparator,我在构造函数中使用比较器时遇到困难。当我尝试以下代码时: InsertionSorter is = new InsertionSorter(bookList.toArray(new Comparable[bookList.size()]), new GenreComparator(), SortType.ASC); 我得到这个错误: no suitable constructor found for InsertionSorter(java.lang.Comparable[],sort.Gen

我在构造函数中使用比较器时遇到困难。当我尝试以下代码时:

InsertionSorter is = new InsertionSorter(bookList.toArray(new Comparable[bookList.size()]), new GenreComparator(), SortType.ASC);
我得到这个错误:

no suitable constructor found for InsertionSorter(java.lang.Comparable[],sort.GenreComparator,sort.SortType)
constructor sort.InsertionSorter.InsertionSorter(java.lang.Comparable[],java.util.Comparator<java.lang.Object>,sort.SortType) is not applicable
  (actual argument sort.GenreComparator cannot be converted to java.util.Comparator<java.lang.Object> by method invocation conversion)
constructor sort.InsertionSorter.InsertionSorter(java.lang.Comparable[],sort.SortType) is not applicable
  (actual and formal argument lists differ in length)
(别担心,我已经进行了检查,以确保在
cmp==null
时不会调用此函数。这会导致以下错误:

method compare in interface java.util.Comparator<T> cannot be applied to given types;
  required: capture#1 of ? extends java.lang.Object,capture#1 of ? extends java.lang.Object
  found: java.lang.Comparable,java.lang.Comparable
  reason: actual argument java.lang.Comparable cannot be converted to capture#1 of ? extends java.lang.Object by method invocation conversion
接口java.util.Comparator中的方法compare不能应用于给定类型; 必需:捕获扩展java.lang.Object中的第1个,捕获扩展java.lang.Object中的第1个 找到:java.lang.Compariable,java.lang.Compariable 原因:实际参数java.lang.Comparable无法转换为捕获#1 of?通过方法调用转换扩展java.lang.Object
请注意,我在AbstractSort.java中更改了上述内容,将
Comparable
替换为
Comparable
GenreComparator
类实现了
Comparator
。因此,您无法传递该类需要
Comparator
的实例。两者都不兼容


但是,您可以将
Comparator
更改为
Comparator
GenreComparator
类实现
Comparator
。因此,您无法传递需要
Comparator
的类的实例。两者都不兼容


不过,您可以将
Comparator
更改为
ComparatorOkay,这解决了我原来的问题,但现在我尝试在
doSort()
方法中使用它时,得到了一些不同的结果。我将对原始帖子进行编辑,以便它实际可用legible@whyles您最好使用
doSort()
method generic.好的,谢谢你的帮助!我制作了
doSort()
generic(这需要在方法中进行一些转换以理顺所有类型),现在一切都进行得很顺利,这解决了我最初的问题,但现在我尝试在
doSort()中使用它时得到了一些不同的东西
method。我将对原始帖子进行编辑,以便它实际上是legible@whyles您最好将
doSort()
方法设置为泛型。好的,谢谢您的帮助!我将
doSort()
设置为泛型(这需要在方法中进行一些转换以理顺所有类型),现在一切都很顺利
public class Book implements Cloneable, Comparable<Book> {
    private Person author; // implementation details of Person don't matter here
    private String title, isbn;
    private int year;
    private BookGenre genre;

    public Book(Person authorInit, String titleInit, String isbnInit, 
            int yearInit, BookGenre genreInit) {
        author = authorInit;
        title = titleInit;
        isbn = isbnInit;
        year = yearInit;
        genre = genreInit;
    }

    @Override
    public String toString() {
        return author.toString() + " " + title + " " + isbn + " " + year 
                + " " + genre;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public int compareTo(Book other) {
        return author.compareTo(other.author);
    }

    public Person getAuthor() {
        return author;
    }

    public String getTitle() {
        return title;
    }

    public String getIsbn() {
        return isbn;
    }

    public int getYear() {
        return year;
    }

    public BookGenre getGenre() {
        return genre;
    }
}

class TitleComparator implements Comparator<Book> {
    @Override
    public int compare(Book a, Book b) {
        return a.getTitle().compareToIgnoreCase(b.getTitle());
    }
}

class GenreComparator implements Comparator<Book> {
    @Override
    public int compare(Book a, Book b) {
        return a.getGenre().compareTo(b.getGenre());
    }
}

enum BookGenre { COMIC, MYSTERY, SCIENCE, TRAVEL };
cmp.compare(values[j-1], values[j])
method compare in interface java.util.Comparator<T> cannot be applied to given types;
  required: capture#1 of ? extends java.lang.Object,capture#1 of ? extends java.lang.Object
  found: java.lang.Comparable,java.lang.Comparable
  reason: actual argument java.lang.Comparable cannot be converted to capture#1 of ? extends java.lang.Object by method invocation conversion