for binarysearch the list has be sorted ascending, for x1 binarysearch has assumed it sorted and got 1 where as for x3 the after reverse the list is still not sorted should give negative value
package q24;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Q24 {
public static void main(String[] args) {
List lst = new ArrayList();
lst.add("e1");
lst.add("e3");
lst.add("e2");
int x1 = Collections.binarySearch(lst, "e3");
System.out.println(x1);
Collections.sort(lst);
int x2 = Collections.binarySearch(lst, "e3");
System.out.println(x2);
Collections.reverse(lst);
int x3 = Collections.binarySearch(lst, "e3");
System.out.println(x3);
}
}
// Result:
// 1
// 2
// -4
upvoted 2 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
SrinivasJasti
2 weeks, 2 days agoxplorerpj
6 months, 3 weeks agojames2033
10 months, 4 weeks ago