我正在尝试通过在类中实现List接口来微调一个List。
我可以轻松地覆盖和实现所有方法,除了这个:
<T> T[] toArray(T[] a);
当覆盖/创建一个新类型T1
时,Android Studio会显示这段代码。为什么会这样,它有什么影响呢?
@NonNull
@Override
public <T1> T1[] toArray(@NonNull T1[] a) {
return inner.toArray(a);
}
当我悬停在“a”参数上时,我看到了这个lint警告:
“应该使用类型为java.lang.Object的数组”
当我试图手动将其改回T []而不是T1 []时,我收到了以下错误:
“QueryList中的toArray(T [])与java.util.List中的toArray(T [])冲突;两种方法具有相同的消除效果,但都没有覆盖另一种方法。”
这是否是Android Studio / IntelliJ中无法正确创建重写的bug,还是我错过了什么?
编辑: 以下是类的声明,包括T及其使用位置。
public class QueryList<T> implements List<T> {
//<editor-fold desc="inner list & constructors">
protected final List<T> inner;
public QueryList() {
inner = new ArrayList<>();
}
public QueryList(int initialCapacity) {
inner = new ArrayList<>(initialCapacity);
}
public QueryList(@NonNull Collection<? extends T> c) {
inner = new ArrayList<>(c);
}
//</editor-fold>
感谢任何帮助。
inner
的声明。谢谢。 - Gennadii Saprykinprotected final List<T> inner;
and in the constructor instanciated asnew ArrayList<>();
- GrisgramT
? - Jorn VerneeQueryList
类声明了一个类型参数,也叫做T
,所以Intellij可能会将覆盖中的那个重命名为T1
,以明确显示它是不同的。 - Jorn VerneeList
接口定义了像它这样的toArray
方法,但它使用的是与List接口不同的类型参数。因此,您可以执行以下操作:Integer[] array = new ArrayList<String>().toArray(new Integer[0]);
- Daniel Beer