简体   繁体   English

Collections.binarySearch不起作用

[英]Collections.binarySearch not working

I have to make a predictive text program and one of the ways I have to do it is by using an ArrayList and the method Collections.binarySearch. 我必须编写一个预测性文本程序,而要做的一种方法是使用ArrayList和Collections.binarySearch方法。 We were told that we had to add a pair all the words from the dictionary and its corresponding numerical signature into the ArrayList using a comparable method, which is this:- 有人告诉我们,必须使用一种可比较的方法将字典中的所有单词及其对应的数字签名添加到ArrayList中,这是这样的:

public class WordSig implements Comparable<WordSig> {
private String word;
private String signature;


public WordSig(String word){
    this.word = word;
    this.signature = ListDictionary.wordToSignature(word);  
}

public String getSignature(){
    return signature;
}


public String toString() {
    return signature + ", " + word;
}

@Override
public int compareTo(WordSig ws){


    return signature.compareTo(((WordSig)ws).signature);

}


}

After doing so I sorted my method and then implemented a method called signatureToWords, that takes a given numerical signature and tries to find the index where that signature is found, however it keeps returning -1. 这样做之后,我对方法进行了排序,然后实现了一个称为signatureToWords的方法,该方法采用给定的数字签名,并尝试查找找到该签名的索引,但是它始终返回-1。 The signatureToWords method is written below:- signatureToWords方法如下所示:

public static Set<String> signatureToWords(String signature) {

    int index = Collections.binarySearch(listOfDictionary, 
            new WordSig(signature));

    System.out.println(index);
    return null;

}

Is there anything you can see in my code that's wrong? 有什么可以在我的代码中看到的吗? I'd really appreciate the help. 我非常感谢您的帮助。 Thanks! 谢谢!

我不会为您做家庭作业,但请确保您没有混淆单词和签名;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM