Oracle offers some java generics questions with solutions. That is drawback 8.
Write a generic methodology to seek out the maximal factor within the vary
[start, finish) of an inventory.
answer
import java.util.*;
public closing class Algorithm {
public static <T extends Object & Comparable<? tremendous T>>
T max(Record<? extends T> listing, int start, int finish) {
T maxElem = listing.get(start);
for (++start; start < finish; ++start)
if (maxElem.compareTo(listing.get(start)) < 0)
maxElem = listing.get(start);
return maxElem;
}
}
Within the methodology signature
public static <T extends Object & Comparable<? tremendous T>> T max(Record<? extends T> listing, int start, int finish)
What’s the level of stating that T should prolong Object? Does not every thing do this anyway? Is that this redundant?