Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement: public List (Comparator<T>comparator){ } The List constructor is pass

ID: 3620781 • Letter: I

Question

Implement: public List (Comparator<T>comparator){ }
The List constructor is passed a comparator, so notice that a comparator class must be written in order to be able to instantiate a List object. The comparator will have a compare () method that will compare 2 objects of Type T, the same type that the list was created to hold, and will return -1, 0, or 1 if one value is smaller than the other, if two values are equal and if one value is larger than the other respectively. The comparator must be used any time that elements of the type stored in the list have to be compared, for example in performing a lookup or insertion operation. This means that the comparator has to be stored by the constructor so that the other list methods can use it later. Other than that, this constructor should appropriately initialize the new list being created so it contains no elements.
So a comparator for Integers will look like this public class IntegerComparator implements Comparator<Integer>{    public int compare(Integer a, Integer b){          return a.compareTo(b);    } } How would I initialize a list with a generic (Comparator <T>comparator) variable?
Implement: public List (Comparator<T>comparator){ }
The List constructor is passed a comparator, so notice that a comparator class must be written in order to be able to instantiate a List object. The comparator will have a compare () method that will compare 2 objects of Type T, the same type that the list was created to hold, and will return -1, 0, or 1 if one value is smaller than the other, if two values are equal and if one value is larger than the other respectively. The comparator must be used any time that elements of the type stored in the list have to be compared, for example in performing a lookup or insertion operation. This means that the comparator has to be stored by the constructor so that the other list methods can use it later. Other than that, this constructor should appropriately initialize the new list being created so it contains no elements.
So a comparator for Integers will look like this public class IntegerComparator implements Comparator<Integer>{    public int compare(Integer a, Integer b){          return a.compareTo(b);    } } How would I initialize a list with a generic (Comparator <T>comparator) variable?

Explanation / Answer

You can initialize a list like List<T> l = new ArrayList<T>()