I have tried many ways to call the countGreaterThan()
function (described here), but I am unable to get it to work. I understand I need to use Integer
instances and not primitives. This is what I've tried:
MainClass.java
public class MainClass {
public static <T extends Comparable<T>> int countGreaterThan(T[] anArray, T elem) {
int count = 0;
for (T e : anArray)
if (e.compareTo(elem) > 0)
++count;
return count;
}
public static void main(String[] args){
Integer[] myArray = {0,1,2,3,4};
Integer myInt = new Integer(2);
int result = countGreaterThan(myArray,myInt);
System.out.println(result);
}
}
This is the interface Comparable.java
public interface Comparable<T> {
public int compareTo(T o);
}
And this is the error I get -
Bound mismatch: The generic method countGreatherThan(T[],T) of type MainClass is not applicable for the arguments(Integer[],Integer). The inferred type Integer is not a valid subsitute for the bounded parameter >