今天因为需要封装一些类,使用了泛型创建数组,方法如下:
T[] container = new T[capacity];
然后编译器报错:
报错Type parameter 'T' cannot be instantiated directly
,即类型参数“T”不能直接实例化。
原因就是Java的泛型是通过类型擦除(type erasure)来实现的。类型擦除简单来说Java在编译期间,所有的泛型信息都会被擦除掉。如在代码中定义的List<object>和List<String>等类型,在编译后都会变成List。JVM看到的只是List,而由泛型附加的类型信息对JVM来说是不可见的。
Java编译器会在编译时尽可能的发现可能出错的地方,但是仍然无法避免在运行时刻出现类型转换异常的情况。直接创建泛型数组不能通过编译,而转型对象数组通过编译但是不能在JVM运行。所以由于类型擦除的原因,Java是禁止直接创建泛型数组实例的
解决方法:使用一个泛型数组包装器,维护一个原始类型的数组,通过数组入口方法进行元素编译期的类型安全检测(对应返回值)和强制类型转换(对于运行时不重要),从而保证类型安全。
public class GenericArray<T> {
private Object[] array; //维护Object[]类型数组
@SupperessWarning("unchecked")
public GenericArray(int sz) {
array = new Object[sz];
}
public void put(int index, T item) {
array[index] = item;
}
public T get(int index) { return (T)array[index]; }//数组对象出口强转
public T[] rep() { return (T[])array; } //运行时无论怎样都是Object[]类型
public static void main (String[] args){
GenericArray<Integer> gai = new GenericArray<Integer>(10);
// Integer[] ia = gai.rep(); //依旧ClassCastException
Object[] oa = gai.rep(); //只能返回对象数组类型为Object[]
gai.put(0,11);
System.out.println(gai.get(0)); // 11 ,出口成功转型
}
}
如果有兴趣了解更多相关内容,欢迎来我的个人网站看看:瞳孔的个人空间