簡體   English   中英

Java Collections Sort不接受帶有arg的比較器構造函數

[英]Java Collections Sort not accepting comparator constructor with 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;
}

}

是否因為我將參數傳遞給比較器而出現錯誤? 我需要吵架

沒有理由不能將參數傳遞給該構造函數。 您的代碼丟失:

  1. 超類。 您的構造函數調用super()所以我假設有一個;

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

numberOfDocs在這里到底意味着什么?

比較器需要比較字符串,因為ArrayList包含字符串。

public class QuerySorter_TFmaxIDF implements Comparator<Term> {  

必須

public class QuerySorter_TFmaxIDF implements Comparator<String> {  

問題出在您的比較器上。 它用於對Term-s進行排序,但是您通過Collections.sort()方法處理的數組具有String類型的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM