I'm trying to write a generic container and I have to use an array. I get the following error :
Exception in thread "main" java.lang.ClassCastException:
[Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
Here is a portion of my code :
class MinFo<T extends Comparable> {
private final T[] array;
MinFo(int size){
array = (T[]) new Object[size];
}
T get(){
if(this instanceof Comparable){
int tmpPos = getNotNull();
T tmp = array[tmpPos];
int pos;
for(pos = tmpPos ; pos < array.length; getNotNull()){
if(tmp.compareTo(array[pos]) > 0)
tmp = array[pos];
}
which led me to remove the "extends Comparable" from the class declaration. Now I have a different error :
error: cannot find symbol
if(tmp.compareTo(array[pos]) > 0)
^
symbol: method compareTo(T)
location: variable tmp of type T
where T is a type-variable:
T extends Object declared in class MinFo
It seems to me that I can't win. Thanks in advance for your help.