问题描述
ArrayList< String>下面是一个问题,第一个代码清单编译得很好(JDK 1.6 | JDK 1.7) a = new ArrayList< String>();
String [] s = a.toArray(new String [0]);
但是,如果我声明 List
参考作为原始类型:
ArrayList a = new ArrayList();
String [] s = a.toArray(new String [0]);
我得到一个编译器错误,指出 String []
是必需的,但找到 Object []
。这意味着我的编译器将通用方法解释为返回 Object []
,尽管接收到 String []
作为它的参数。
我对进行了双重检查toArray(myArray)
方法签名:
< T> T [] toArray(T [] a);
因此,它是一个参数化方法,其类型参数 我不知道如何在这里使用原始类型影响使用独立类型参数的参数化方法的评估。 这并不完全符合您的期望,但如果您以原始形式引用泛型类,则会失去对实例成员使用泛型的能力。它不限于泛型方法,请检查这一点: 这是JLS的相关部分(): Here's a question, this first code listing compiles just fine (JDK 1.6 | JDK 1.7): However, if I declare the I get a compiler error saying the This means my compiler is interpreting the generic method as returning I doubled-checked the Therefore it is a parameterized method whose type parameter I have no idea how using a raw type here affects the evaluation of parameterized methods using independent type parameters. < T> code>与列表(即
< E>
)没有任何关系。
public class MyContainer< T> {
公开列表< String>字符串(){
返回Arrays.asList(a,b);
}
}
MyContainer container = new MyContainer< Integer>();
列表< String> strings = container.strings(); //给出未经检查的警告!
ArrayList<String> a = new ArrayList<String>();
String[] s = a.toArray(new String[0]);
List
reference as a raw type:ArrayList a = new ArrayList();
String[] s = a.toArray(new String[0]);
String[]
is required but Object[]
was found. Object[]
despite of receiving a String[]
as its argument.toArray(myArray)
method signature:<T> T[] toArray(T[] a);
<T>
has no relation whatsoever with that of the List (i.e. <E>
).
It's not exactly what you'd expect, but if you refer to a generic class in raw form, you lose the ability to use generics in any way for instance members. It's not restricted to generic methods either, check out this:
public class MyContainer<T> {
public List<String> strings() {
return Arrays.asList("a", "b");
}
}
MyContainer container = new MyContainer<Integer>();
List<String> strings = container.strings(); //gives unchecked warning!
This is the relevant part of the JLS (4.8):
这篇关于结合原始类型和通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!