Java中泛型创建数组的总结


在java中,不能通过直接通过T[] tarr=new T[10]的方式来创建数组,最简单的方式便是通过Array.newInstance(Class<t>type,int size)的方式来创建数组例如下面的程序

 
  1. public class ArrayMaker<T> {  
  2.     private Class<T> type;  
  3.   
  4.     public ArrayMaker(Class<T> type) {  
  5.         this.type = type;  
  6.     }  
  7.   
  8.     @SuppressWarnings("unchecked")  
  9.     T[] createArray(int size) {  
  10.         return (T[]) Array.newInstance(type, size);  
  11.     }  
  12.   
  13.     List<T> createList() {  
  14.         return new ArrayList<T>();  
  15.     }  
  16.   
  17.     /** 
  18.      * @param args 
  19.      */  
  20.     public static void main(String[] args) {  
  21.         /* 
  22.          * Even though kind is stored as Class<T> , erasure means that it is actually just being stored as a Class, with 
  23.          * no parameter. So, when you do some thing with it, as in creating an array, Array.newInstance( ) doesn’t 
  24.          * actually have the type information that’s implied in kind; so it cannot produce the specific result, wh ich 
  25.          * must therefore be cast, which produces a warning that you cannot satisfy. 
  26.          */  
  27.         ArrayMaker<Type> am2 = new ArrayMaker<Type>(Type.class);  
  28.         System.out.println(Arrays.asList(am2.createArray(10)));  
  29.         System.out.println(Arrays.asList(am2.createList()));  
  30.     }  
  31.   
  32. }  
  33.   
  34. class Type {  
  35.     @Override  
  36.     public String toString() {  
  37.         return "type";  
  38.     }  
  39. }  

上面的这个例子比较简单,但是如果你有接触过泛型数组,你便对他的复杂度有一定的了解,由于创建泛型数组比较复杂,所以在实际的应用过程中一般会选择List的对泛型进行存储,如果实在需要使用泛型数组,则需要注意数组的在运行时的类型,think in java这本书中,对泛型数组的处理通过四个小程序对其进行了比较完整的描述

程序一:这个程序主要说明了,在使用泛型数组中容易出现的问题,由于书中对于程序的说明比较详细,所以只对程序做引用

 
  1. class Generic<T> {  
  2. }  
  3.   
  4. public class ArrayofGeneric {  
  5.     public static void main(String[] args) {  
  6.         Generic<Integer>[] genArr;  
  7.         /* 
  8.          * will throw ClassCastException :The problem is that arrays keep track of their actual type, and that type is 
  9.          * established at the point of creation of the array. So even though genArr has been cast to a Generic < Integer 
  10.          * >[] , that information only exists at compile time (and without the @SuppressWarnings annotation, you’d get a 
  11.          * warning for that cast). At run time, it’s still an array of Object, and that causes problems. 
  12.          */  
  13.         // genArr = (Generic<Integer>[]) new Object[] {};   
  14.         /* can not create a generic of array */  
  15.         // genArr=new Generic<Integer>[2];   
  16.         genArr = (Generic<Integer>[]) new Generic[2];  
  17.         System.out.println(genArr);  
  18.     }  
  19. }  

程序二:这个程序主要是说明在程序的执行过程中,泛型数组的类型信息会被擦除,且在运行的过程中数组的类型有且仅有Object[],如果我们强制转换成T[]类型的话,虽然在编译的时候不会有异常产生,但是运行时会有ClassCastException抛出

 
  1. /** 
  2.  *  
  3.  * Because of erasure, the runtime type of the array can only be Object[]. If we immediately cast it to T[], then at 
  4.  * compile time the actual type of the array is lost, and the compiler may miss out on some potential error checks. 
  5.  *  
  6.  *  
  7.  *  
  8.  * archive $ProjectName: $ 
  9.  *  
  10.  * @author Admin 
  11.  *  
  12.  * @version $Revision: $ $Name: $ 
  13.  */  
  14. public class ArrayOfGeneric2<T> {  
  15.     public T[] ts;  
  16.   
  17.     public ArrayOfGeneric2(int size) {  
  18.         ts = (T[]) new Object[size];  
  19.     }  
  20.   
  21.     public T get(int index) {  
  22.         return ts[index];  
  23.     }  
  24.   
  25.     public T[] rep() {  
  26.         return ts;  
  27.     }  
  28.   
  29.     public void set(int index, T t) {  
  30.         ts[index] = t;  
  31.     }  
  32.   
  33.     public static void main(String[] args) {  
  34.         ArrayOfGeneric2<String> aog2 = new ArrayOfGeneric2<String>(10);  
  35.         Object[] objs = aog2.rep();  
  36.         System.out.println(objs);  
  37.         /* will throw ClassCastException */  
  38.        // String[] strs = aog2.rep();   
  39.         // System.out.println(strs);   
  40.     }  
  41.   
  42. }  

  • 1
  • 2
  • 下一页

相关内容