目录

Java泛型 - 没有数组( No Array)

不允许使用参数化类型的数组。

//Cannot create a generic array of Box<Integer>
Box<Integer>[] arrayOfLists = new Box<Integer>[2]; 

由于编译器使用类型擦除,因此type参数将替换为Object,用户可以将任何类型的对象添加到数组中。 并且在运行时,代码将无法抛出ArrayStoreException。

// compiler error, but if it is allowed
Object[] stringBoxes = new Box<String>[];
// OK
stringBoxes[0] = new Box<String>();  
// An ArrayStoreException should be thrown,
//but the runtime can't detect it.
stringBoxes[1] = new Box<Integer>();  
↑回到顶部↑
WIKI教程 @2018