dimanche 12 février 2017

Arrays.binarySearch(Object[], key) returning wrong value? (Java)

Vote count: 0

public static boolean isEnglishWord(String[] dictionary)
{
    boolean isEnglishWord = true;
    System.out.println(dictionary[466860]);
    String word = "pentail";
    Arrays.sort(dictionary);
    int index = Arrays.binarySearch(dictionary, word);
    if(index < 0)
    {
        isEnglishWord = false;
        System.out.println(index);
    }
    return isEnglishWord;       
}

I want this function to take in a String, and tell me if it's a valid English word by finding it's index in the dictionary. For some reason the binary search is returning the negative equivalent of the index I would expect.

I was under the impression if the word was in the dictionary it would return the index, and if it wasn't it would return the negative value of where it should be placed.

Any help would be appreciated, thanks.

asked just now

Let's block ads! (Why?)



Arrays.binarySearch(Object[], key) returning wrong value? (Java)

Aucun commentaire:

Enregistrer un commentaire