好吧,这是我的问题: 我正在使用 ArrayLinearList 类来创建不同的数组类型。现在,我必须创建一个 boolean 方法:compare(Linearlist other) 并且 LinearList 是 ArrayLinearList 的接口(interface)。
当我使用比较时,如果比较的元素不同,则此方法应该返回 false。但我不知道该怎么做,因为元素的其他参数是一个接口(interface),而要比较的数组是 T[] 数组类型,所以当我使用该方法时,它应该如下所示: 元素比较(其他);
那么如何将一个LinearList对象,其中LinearList是一个接口(interface),转换成一个t[]类型的对象呢??
public boolean compare(LinearList other){
ArrayLinearList<T> element2 = other;
// cast try:
Integer size1 = other.size();
Integer size2 = 0;
for (int i = 0; i< size; i++)
size2++;
if (size2 == size1)
return true;
else return false;
//else return false;
}
然后我得到这个错误:error: incompatible types: LinearList<CAP#1> cannot be converted to ArrayLinearList<T>
ArrayLinearList<T> element2 = other;
^
where T is a type-variable:
T extends Comparable<T> declared in class ArrayLinearListTaller1
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
1 error
那么,我怎样才能将 other 转换为元素类型 T[],或者我怎样才能比较它们?
如果有人能帮助我,我将不胜感激¡¡¡:)
最佳答案
声明应该是 or public boolean compare(LinearList<? extends T> other)
,以便编译器知道它从 other
检索的对象将是 T
类型或其子类型。
关于java - 比较方法和泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22599161/