I'm really stuck on an assignment for school. We are learning about Generic Types, and maybe this is me not just understanding them fully, but as part of one of the first methods we have to implement:
We have:
public static <T> T min(Collection<T> c, Comparator<T> comp) {
return null
}
And the requirements:
Selects the minimum value from the Collection c
, as defined by the
supplied Comparator comp
. This method throws an IllegalArgumentException
if either c or comp is null, and it throws a NoSuchElementException
if c
is empty. The Collection c is not changed by this method.
So I've gotten to here:
public static <T> T min(Collection<T> c, Comparator<T> comp)
throws IllegalArgumentException, NoSuchElementException {
if (c != null && comp != null) {
if (!c.isEmpty()) {
} else {
throw new NoSuchElementException();
}
} else {
throw new IllegalArgumentException();
}
}
We have to sort using a comparator, but CANNOT use the Collections class. I really just need some direction to start, I'm not asking you to do the assignment for me!
Collections.sort
by using theArrays
version is probably not what the instructor had in mindcompareTo
and keep the instance that evaluates less than. At the end of iteration return the instance that remains.T extends Comparable
is not needed. Do not confuseComparable
withComparator