2014-03-31 49 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