Domanda

I'm having difficulty using Comparators in Constructors. When I try the following code:

InsertionSorter is = new InsertionSorter(bookList.toArray(new Comparable[bookList.size()]), new GenreComparator(), SortType.ASC);

I get this error:

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)

Here are the useful code snippets:

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

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

EDIT Thanks to Rohit for a solution to this problem, but now I'm having a related problem. In the method doSort(), I have the code snippet

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

(don't worry, I have checks in place to make sure this isn't called if cmp == null. This gives the following error:

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

Note that I have changed the above in AbstractSort.java to replace Comparable<Object> with Comparable<? extends Object>

È stato utile?

Soluzione

GenreComparator class implements Comparator<Book>. So you can't pass an instance of that class where a Comparator<Object> is required. Both are non-compatible.

You can however change Comparator<Object> to Comparator<? extends Object> in your constructor, and also the type of field cmp in AbstractSorter class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top