Java Collections Sort 不接受带有 arg 的比较器构造函数
我收到此行的编译器错误:
Collections.sort(terms, new QuerySorter_TFmaxIDF(myInteger));
我的自定义比较器非常基本;这是签名和构造函数:
public class QuerySorter_TFmaxIDF implements Comparator<Term>{
private int numberOfDocs;
QuerySorter_TFmaxIDF(int n){
super();
numberOfDocs = n;
}
}
是否因为我将参数传递到比较器而出现错误?我需要传递一个论点...
I'm getting a compiler error for this line:
Collections.sort(terms, new QuerySorter_TFmaxIDF(myInteger));
My customized Comparator is pretty basic; here's the signature and constructor:
public class QuerySorter_TFmaxIDF implements Comparator<Term>{
private int numberOfDocs;
QuerySorter_TFmaxIDF(int n){
super();
numberOfDocs = n;
}
}
Is there an error because I'm passing an argument into the Comparator? I need to pass an argument...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有理由不能将参数传递给该构造函数。您的代码丢失:
超类。你的构造函数调用
super()
所以我假设有一个;和Comparator 接口所需的
compare()
方法。numberOfDocs
在这里到底要做什么?There's no reason you can't pass an argument to that constructor. Your code is missing:
The superclass. Your constructor calls
super()
so I assume there is one; andThe
compare()
method required by theComparator
interface.What exactly is
numberOfDocs
meant to do here?您的比较器需要比较字符串,因为您的 ArrayList 包含字符串。
必须是
Your Comparator needs to compare Strings because your ArrayList contains Strings.
has to be
问题出在你的比较器上。它用于对 Term-s 进行排序,但您通过 Collections.sort() 方法传递的数组具有 String 类型的元素。
The problem lies with your comparator. It's for sorting Term-s but the array you're handing it via the Collections.sort() method has elements of type String.