0

While learning Java Generics parametrization I came up to this code:

public interface Comparable<T> {
    public int compareTo(T o);
}

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;
}

I wanted to test it, so I wrote this:

Integer[] iArray = new Integer[10];
for (int i=0; i<10; i++){
    iArray[i] = new Integer(i);
}

int a = countGreaterThan(iArray, Integer.valueOf(5));

but compiler is giving me error message on last line when calling a method countGreaterThan:

The method countGreaterThan(T[], T) in the type Main is not applicable for the arguments (Integer[], Integer)

Am I missing something obvious?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
ajuric
  • 19
  • 2

1 Answers1

5

java.lang.Integer does not implement the Comparable interface that you just wrote.

You should delete that interface and use the built-in one, which it does implement.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964