在java中,可以声明一个泛型数组不能通过直接通过T[] tarr=new T[10]的方式来创建数组,最简单的方式便是通过Array.newInstance(Classtype,int size)的方式来创建数组例如下面的程序。

  1. public class ArrayMaker<T> {

    private Class<T> type;

    public ArrayMaker(Class<T> type) {
    this.type = type;
    }

    @SuppressWarnings("unchecked")
    T[] createArray(int size) {
    return (T[]) Array.newInstance(type, size);
    }

    List<T> createList() {
    return new ArrayList<T>();
    }

    /** * @param args */
    public static void main(String[] args) {
    /** Even though kind is stored as Class<T> , erasure means that it is actually just being stored as a Class, with
    * no parameter. So, when you do some thing with it, as in creating an array, Array.newInstance( ) doesn’t
    * actually have the type information that’s implied in kind; so it cannot produce the specific result, which
    * must therefore be cast, which produces a warning that you cannot satisfy.
    */
    ArrayMaker<Type> am2 = new ArrayMaker<Type>(Type.class);
    System.out.println(Arrays.asList(am2.createArray(10)));
    System.out.println(Arrays.asList(am2.createList()));
    }
    }
    class Type {
    @Override
    public String toString() {
    return "type";
    }
    }

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

  1. class Generic<T> {    

  }   

  public class ArrayofGeneric {    

    public static void main(String[] args) {    

       Generic<Integer>[] genArr;  

       genArr = (Generic<Integer>[]) new Generic[2];    

       System.out.println(genArr);    

   }    

}  

  1.  

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

  1. public class ArrayOfGeneric2<T> {
  2. public T[] ts;
  3. public ArrayOfGeneric2(int size) {
  4. ts = (T[]) new Object[size];
  5. }
  6. public T get(int index) {
  7. return ts[index];
  8. }
  9. public T[] rep() {
  10. return ts;
  11. }
  12. public void set(int index, T t) {
  13. ts[index] = t;
  14. }
  15. public static void main(String[] args) {
    ArrayOfGeneric2<String> aog2 = new ArrayOfGeneric2<String>(10);
  16. Object[] objs = aog2.rep();
  17. System.out.println(objs);
  18. /* will throw ClassCastException */ // String[] strs = aog2.rep(); //System.out.println(strs);
  19. }
  20. }

    程序三:主要说明在对象中通过用Object[]来保存数据,则生成对象是,可以对其持有的对象在T和object之间进行转换,但是当设计到数组的转换时,还是会报ClassCastException

  1. public class ArrayOfGeneric3<T> {
  2. Object[] ts;
  3. public ArrayOfGeneric3(int size) {
  4. ts = new Object[size];
  5. }
  6. public T get(int index) {
  7. return (T) ts[index];
  8. }
  9. public T[] rep() {
  10. return (T[]) ts;
  11. }
  12. public void set(int index, T t) {
  13. ts[index] = t;
  14. }
  15. public static void main(String[] args) {
    ArrayOfGeneric3<Integer> aog2 = new ArrayOfGeneric3<Integer>(10);
  16. Object[] objs = aog2.rep();
  17. for (int i = 0; i < 10; i++) {
  18. aog2.set(i, i);
  19. System.out.println(aog2.get(i));
  20. }
  21. Integer[] strs = aog2.rep();
  22. System.out.println(strs);
  23. }
  24. }

程序四:是对泛型数组相对而言比较完美的解决方案

  1. public class ArrayOfGeneric4<T> {
  2. T[] ts;
  3. public ArrayOfGeneric4(Class<T> type, int size) {
  4. /* to solution array of generic key code! */
  5. ts = (T[]) Array.newInstance(type, size);
  6. }
  7. public T get(int index) {
  8. return ts[index];
  9. }
  10. public T[] rep() {
  11. return ts;
  12. }
  13. public void set(int index, T t) {
  14. ts[index] = t;
  15. }
  16. public static void main(String[] args) {
    ArrayOfGeneric4<Integer> aog2 = new ArrayOfGeneric4<Integer>(Integer.class, 10);
  17. Object[] objs = aog2.rep();
  18. for (int i = 0; i < 10; i++) {
  19. aog2.set(i, i);
  20. System.out.println(aog2.get(i));
  21. }
  22. try {
  23. Integer[] strs = aog2.rep();
  24. System.out.println("user Array.newInstance to create generci of array was successful!!!!! ");
  25. } catch (Exception ex) {
  26. ex.printStackTrace();
  27. }
  28. }
  29. }

原文请见http://developer.51cto.com/art/201202/317813.htm

版权声明:本文为onlysun原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/onlysun/p/4531604.html