Java Collections Sort 不接受带有 arg 的比较器构造函数

发布于 2024-08-29 14:47:31 字数 376 浏览 2 评论 0原文

我收到此行的编译器错误:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁吢 2024-09-05 14:47:32

没有理由不能将参数传递给该构造函数。您的代码丢失:

  1. 超类。你的构造函数调用 super() 所以我假设有一个;和

  2. Comparator 接口所需的 compare() 方法。

numberOfDocs 在这里到底要做什么?

There's no reason you can't pass an argument to that constructor. Your code is missing:

  1. The superclass. Your constructor calls super() so I assume there is one; and

  2. The compare() method required by the Comparator interface.

What exactly is numberOfDocs meant to do here?

揽月 2024-09-05 14:47:32

您的比较器需要比较字符串,因为您的 ArrayList 包含字符串。

public class QuerySorter_TFmaxIDF implements Comparator<Term> {  

必须是

public class QuerySorter_TFmaxIDF implements Comparator<String> {  

Your Comparator needs to compare Strings because your ArrayList contains Strings.

public class QuerySorter_TFmaxIDF implements Comparator<Term> {  

has to be

public class QuerySorter_TFmaxIDF implements Comparator<String> {  
零時差 2024-09-05 14:47:32

问题出在你的比较器上。它用于对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文