What is the result?

Given the integer implements comparable:

import java.util.*;

public class SortAndSearch2 {

static final Comparator<Integer> IntegerComparator =

new Comparator<Integer>() {

public int compare (Integer n1, Integer n2) {

return n2.compareTo(n1);

}

};

public static void main(String args[]) {

ArrayList<Integer> list = new ArrayList<>();

list.add (4);

list.add (1);

list.add (3);

list.add (2);

Collections.sort(list, null);

System.out.println(Collections.binarySearch(list, 3));

Collections.sort(list,IntegerComparator);

System.out.println(Collections.binarySearch(list, 3));

}

}

What is the result?

Given the integer implements comparable:

import java.util.*;

public class SortAndSearch2 {

static final Comparator<Integer> IntegerComparator =

new Comparator<Integer>() {

public int compare (Integer n1, Integer n2) {

return n2.compareTo(n1);

}

};

public static void main(String args[]) {

ArrayList<Integer> list = new ArrayList<>();

list.add (4);

list.add (1);

list.add (3);

list.add (2);

Collections.sort(list, null);

System.out.println(Collections.binarySearch(list, 3));

Collections.sort(list,IntegerComparator);

System.out.println(Collections.binarySearch(list, 3));

}

}

What is the result?

A.
2

B.
1

C.
2

D.
3



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Kamil

Kamil

Result is: 2 1

admin

admin

Thanks Kamil.