public class ArrayImplementation <Element> implements Iterable<Element>{
private Element [] list;
public ArrayImplementation (final int initSize)
{
list = (Element[]) new Object[initSize];
this.size = initSize;
}
private boolean increaseSorted()
{
for (int i=1;i<length;i++)
{
if (list[i].compareTo(list[i-1])<0)
{
return false;
}
}
return true;
}
}
我如何让Element使用compareTo方法?
如果我写成
public class ArrayImplementation <Element extends Comparable> implements Iterable<Element>{
public ArrayImplementation (final int initSize)
{
list = (Element[]) new Object[initSize];
this.size = initSize;
}
private boolean increaseSorted()
{
for (int i=1;i<length;i++)
{
if (list[i].compareTo(list[i-1])<0)
{
return false;
}
}
return true;
}
}
让Element继承Comparable,就会报错
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
我要怎么写呢?