【发布时间】:2015-07-01 08:30:35
【问题描述】:
我们不能创建泛型类型的数组,这是众所周知的事实,所以我不会在这里提供对 JLS 的正式引用。但是我们可以声明这样的数组如下:
static <E> void reduce() {
List<Integer>[] arr; //compiles fine
E[] avv; //compiles fine
avv = new E[10]; //doesn't compile
arr = new List<Integer>[10]; //doesn't compile
}
有人知道这种声明的原因吗?
【问题讨论】:
-
如果你不能声明一个泛型数组(作为变量、字段或方法参数),那么一个类怎么能使用泛型呢?