简体   繁体   中英

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. 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:-

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. The signatureToWords method is written below:-

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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