Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.93 :

Posted: Tue Jan 25, 2011 9:14 pm
by ETS User

Code: Select all

class MyStringComparator implements Comparator
{
    public int compare(Object o1, Object o2)
    {
	int s1 = ((String) o1).length();
	int s2 = ((String) o2).length();
	return s1 - s2;
    }
}

and 

static String[] sa = { "d", "bbb", "aaaa" };
The second option is one of the correct selections to this question, and it states:
Arrays.binarySearch(sa, "cc", new MyStringComparator()); will return -2.
Since there is no string of length 2 in sa, nothing in sa matches aa. So the return value has to be negative. Further, if aa were to be inserted in sa, it would have to be inserted after 'a' i.e. at index 1. Thus, the return value will be -(index+1) = -2.
....nothing in sa matches aa....Further, if aa were to be inserted....
I am confused. Where is "aa" mentioned in the code for this question, or are you referring to "cc" which is what is stated in this answer selection?

Re: About Question com.enthuware.ets.scjp.v6.2.93 :

Posted: Wed Jan 26, 2011 6:53 am
by admin
You are right. "aa" is actually "cc". The explanation has been fixed to the following:

Since there is no string of length 2 in sa, nothing in sa matches the string "cc". So the return value has to be negative. Further, if the values "cc" were to be inserted in sa, it would have to be inserted after "a" i.e. at index 1. Thus, the return value will be -(index+1) = -2.

Re: About Question com.enthuware.ets.scjp.v6.2.93 :

Posted: Sat Mar 26, 2011 1:57 pm
by mark
I still see a problem with this revised answer. There is no 'a' in the problem. Since the sort order is 'd', 'bbb', and 'dddd', don't you mean 'd'? Shouldn't the answer read "Further, if the values "cc" were to be inserted in sa, it would have to be inserted after "d" ;) i.e. at index 1. Thus, the return value will be -(index+1) = -2."

Re: About Question com.enthuware.ets.scjp.v6.2.93 :

Posted: Sat Mar 26, 2011 7:23 pm
by admin
Yes, it should be 'd'. Fixed. Thanks again for your feedback.