What are all possible values program can print when run?
import java.util.*;
public class TestClass {
static String[] sa = { "a", "aa", "aaa", "aaaa" };
static { Arrays.sort(sa); }
public static void main(String[] args) {
String search = "";
if(args.length != 0) search = args[0];
System.out.println(Arrays.binarySearch(sa, search));
}
}
The correct answer is Any number from -5 to 3.
I still don't understand the correct answer.
There are three possibilities.
1) if all elements in the array less than the search key, the insertion point is 4, thus -5 is returned.
2) if all elements in the array greater than the search key, the insertion point is 0, thus -1 is returned.
3) if any element in the array matches the search key, the value returned should range between -5 and -1.
So how can the values from 0 to 3 be returned?