Reputation: 13
I get an error on eclipse when calling the method maximum, saying the method maximum (<? extends T>, int, int) in the type generics classname is not applicable for the argument (List<Integer>, int ,int).
how can i solve this error
public class Generics_oracle_question {
public static void main(String args[]) {
List<Integer>[] integer = new List[5];
integer[0].add(6);
integer[1].add(3);
integer[2].add(9);
System.out.println(maximum(integer,0,2));
}
public static<T extends Comparable> T maximum(List<? extends T> elements,int beg, int end) {
T max = elements.get(beg);
for(; beg <= end; beg++) {
if(max.compareTo(elements.get(beg)) < 0) {
max = elements.get(beg);
}
}
return max;
}
}
Upvotes: 1
Views: 58
Reputation: 726539
The problem is that you are passing an array of lists to a method that takes a list.
Here is how you instantiate and populate a List<Integer>
:
List<Integer> integer = new ArrayList<>();
integer.add(6);
integer.add(3);
integer.add(9);
Now your code compiles and runs, correctly returning 9 (demo).
Upvotes: 2