Fill in the blank to make this code compile:
public class Main implements Comparable<Main> { private int id; public Main(int id) { this.id = id; }//from w w w . j a v a 2 s . c o m @Override { return id - t.id; } }
A. public int compare(Main t) B. public int compare(Main t1, Main t2) C. public int compareTo(Main t) D. public int compareTo(Main t1, Main t2)
C.
When implementing Comparable, you implement the compareTo()
method.
Since this is an instance method, it already has a reference to itself and only needs the item it is comparing.
Only one parameter is specified, and Option C is correct.
The Comparator interface uses the compare()
method and the method takes two parameters.