Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Binary search in java?

ID: 3608928 • Letter: B

Question

import java.util.Arrays;
public boolean isValidPrefix(String prefixToCheck)
{
if ((java.util.Arrays.binarySearch(dictiona...prefixToCheck))>=0)
{
return true;
}
return false;
}

I’m wondering why the return of this binary search is -65537,I was expecting to be a positive number since the StringprefixToCheck test is in the dictionary array I created. thedictionary is like this:
gyve
gyved
gyves
gyving
ha
haaf
haafs
haar
haars
habanera
habaneras
habdalah
habdalahs
haberdasher
haberdasheries
haberdashers


I wonder if that method only works if it needs the whole word towork
if it is just a string "h" will it work? and return a positivenumber?
i think so, but what do you think? if not what can I do to searchmy array dictionary[] to find the prefix in a dictionary?

Explanation / Answer

please rate - thanks I've never done this, but looking through the definitions at http://java.sun.com/javase/6/docs/api/java/util/Arrays.html it will return a negative number if not found.   And it would have to match the entire string prefixToCheck tomatch, not just a character or 2. Hope this puts you in the right direction