2014-03-31 44 views
2

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

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) 

这里是有用的代码片段:

AbstractSorter.java:

abstract class AbstractSorter { 
    protected Comparable[] values; 
    protected Comparator<Object> cmp; 
    protected SortType st; 

    protected abstract void doSort(); 
    protected AbstractSorter(Comparable[] init, SortType type) { 
     values = new Comparable[init.length]; 
     System.arraycopy(init, 0, values, 0, init.length); 
     cmp = null; 
     st = type; 
    } 
    protected AbstractSorter(Comparable[] init, Comparator<Object> comp, SortType type) { 
     values = new Comparable[init.length]; 
     System.arraycopy(init, 0, values, 0, init.length); 
     cmp = comp; 
     st = type; 
    } 

    public Comparable[] getValues() { 
     return values; 
    } 
} 

class InsertionSorter extends AbstractSorter { 

    public InsertionSorter(Comparable[] init, SortType type) { 
     super(init, type); 
    } 

    public InsertionSorter(Comparable[] init, Comparator<Object> comp, SortType type) { 
     super(init, comp, type); 
    } 

    @Override 
    public void doSort() { 
     // sorts values 
    } 
} 

enum SortType {ASC, DSC}; 

书。 java:

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 }; 

编辑感谢Rohit解决这个问题,但现在我有一个相关的问题。在该方法中doSort(),我的代码片断

cmp.compare(values[j-1], values[j]) 

(别担心,我有地方检查,以确保这不叫,如果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 

请注意,我已经改变了上面AbstractSort.java与Comparable<? extends Object>

回答

4

GenreComparator类来代替Comparable<Object>实现Comparator<Book>,所以你不能传递的一个实例该类需要Comparator<Object>。两者都不兼容。

但是,您可以在AbstractSorter类改变Comparator<Object>Comparator<? extends Object>在你的构造,并且还现场cmp的类型。

+0

好的,这解决了我原来的问题,但现在当我尝试在'doSort()'方法中使用它时,我得到了不同的东西。我将对原始文章进行编辑,使其实际易读 – wlyles

+0

@whyles将doSort()方法设为通用,会更好。 –

+0

好的,谢谢你的帮助!我制作了'doSort()'通用的(这需要在方法中进行一些转换来理清所有类型),现在一切都很好地流动 – wlyles