我正在尝试创建一个类,我可以在二进制搜索的帮助下快速将对象插入到类列表中。
这是我的班级和内部班级:
public class PostingsList implements Iterator<PostingsEntry>{
/** The postings list */
private ArrayList<PostingsEntry> list = new ArrayList<PostingsEntry>();
class PostingsEntryComparator implements Comparator{
@Override
public int compare(PostingsEntry pA, PostingsEntry pB){
if(pA.docID < pB.docID){
return -1;
}
else if(pA.docID == pB.docID){
return 0;
}
else{
return 1;
}
}
}
public void add(PostingsEntry newPostingsEntry){
//put in the right place
PostingsEntryComparator pc = new PostingsEntryComparator();
int res = Collections.binarySearch(list, newPostingsEntry, pc);
if(res < 0){
list.add(-res-1, newPostingsEntry);
}
else{
System.out.println("already exists");
}
}
}
内部类用于比较list
的对象,以便Collections.binarySearch
可以工作。但是,我收到了这个错误。它是什么意思,我该怎么办呢?
n180-p69:new sahandzarrinkoub$ sh compile_all.sh
ir/PostingsList.java:22: error: PostingsList.PostingsEntryComparator is not abstract and does not override abstract method compare(Object,Object) in Comparator
class PostingsEntryComparator implements Comparator{
^
ir/PostingsList.java:23: error: method does not override or implement a method from a supertype
@Override
^
2 errors
答案 0 :(得分:7)
class PostingsEntryComparator implements Comparator {
...
}
实施&#34; raw&#34; Comparator
接口,接受Object
个参数,即compare(Object,Object)
。但是,这不是您要覆盖的内容,因为您的比较器特定于PostingsEntry
。
因此,您需要实施Comparator<PostingsEntry>
:
class PostingsEntryComparator implements Comparator<PostingsEntry> {
...
}