没有为sort()找到合适的方法...从Comparator接口

时间:2017-09-12 16:54:39

标签: java compare comparator

我正在尝试为我的" WordData"实施比较方法。对象。我想比较对象的两个String字段,它们是" word" 请看下面。 这是我的比较方法实现:

 Arrays.sort(list, new Comparator<WordData>(){

        @Override
        public int compare(WordData o1, WordData o2){
            return o1.getWord().compareTo(o2.getWord());
        }

    });

这是我的WordData类:

public class WordData {

private String word;
private int counter =0;
private String[] positions = new String[1];

public WordData(String word, int position, int lineNo, int counter) {
    this.word=word;
    this.counter=counter;
    positions[0]=String.valueOf(position)+", "+String.valueOf(lineNo);
}


public String getWord() {
    return word;
}}

我在compare()方法中收到错误消息,如:

Error:(62, 15) java: no suitable method found for sort(java.util.List<PrintTest.WordData>,<anonymous java.util.Comparator<PrintTest.WordData>>)
    method java.util.Arrays.<T>sort(T[],java.util.Comparator<? super T>) is not applicable
      (cannot infer type-variable(s) T
        (argument mismatch; java.util.List<PrintTest.WordData> cannot be converted to T[]))
    method java.util.Arrays.<T>sort(T[],int,int,java.util.Comparator<? super T>) is not applicable
      (cannot infer type-variable(s) T
        (actual and formal argument lists differ in length))

0 个答案:

没有答案