【发布时间】:2019-06-22 14:09:17
【问题描述】:
我知道数组泛型数组创建是不允许的,因为数组需要在运行时知道它们的类型,但由于泛型在运行时擦除它们的类型信息,所以不可能创建泛型数组。 但是它为什么允许通用数组声明如下:
private E[] genericArray;// this line does not prevent the class from compiling
private E[] genericArrayTwo= new E[][10];// this line prevents the class from compiling
【问题讨论】:
-
为什么不允许?注意,数组的字段声明默认初始化为
null,所以不需要知道元素类型。
标签: java generic-programming generic-list