1、可以创建,但是由于创建泛型数组比较复杂,所以在实际的应用过程中一般会选择List的对泛型进行存储
2、有这么一个例子可以帮你理解,书上的- class Generic { }
- public class ArrayofGeneric {
- public static void main(String[] args) {
- Generic[] genArr; /* * will throw ClassCastException :The problem is that arrays keep track of their actual type, and that type is * established at the point of creation of the array. So even though genArr has been cast to a Generic < Integer * >[] , that information only exists at compile time (and without the @SuppressWarnings annotation, you’d get a * warning for that cast). At run time, it’s still an array of Object, and that causes problems. */
- // genArr = (Generic[]) new Object[] {}; /* can not create a generic of array */ //
- genArr=new Generic[2]; genArr =(Generic[]) new Generic[2];
- System.out.println(genArr); } }
复制代码 --------------------------------------------------------
主要是说明在程序的执行过程中,泛型数组的类型信息会被擦除,且在运行的过程中数组的类型有且仅有Object[],如果我们强制转换成T[]类型的话,虽然在编译的时候不会有异常产生,但是运行时会有ClassCastException抛出
3、泛型的本质就是参数化类型,就是说所操作的数据类型被指定为一个参数,这种参数类型可以用在类,接口,方法的创建中,也就是泛型类,泛型接口,泛型方法。 |