Given:
3. import java.util.*; 4. public class Main implements Comparator<Main> { 5. int dishSize; 6. public static void main(String[] args) { 7. Main[] va = {new Main(40), new Main(200), new Main(60)}; 8. /*from www . j av a 2 s . c o m*/ 9. Arrays.sort(va, va[0]); 10. int index = Arrays.binarySearch(va, new Main(40), va[0]); 11. System.out.print(index + " "); 12. index = Arrays.binarySearch(va, new Main(80), va[0]); 13. System.out.print(index); 14. } 15. public int compare(Main a, Main b) { 16. return b.dishSize - a.dishSize; 17. } 18. Main(int d) { dishSize = d; } 19. }
What is the result?
D is correct.
The compare()
method has swapped the normal ordering, so "arrays of Main" sort in descending sequence.
Second, of course, is that when binarySearch()
can't find an element it returns (-(insertion point) -1) to indicate where the element would have been placed if it had been found.
Note that on line 9, we're sorting based on a Comparator (the 2nd argument).