歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java中泛型創建數組的總結

Java中泛型創建數組的總結

日期:2017/3/1 10:31:17   编辑:Linux編程

在java中,不能通過直接通過T[] tarr=new T[10]的方式來創建數組,最簡單的方式便是通過Array.newInstance(Class<t>type,int size)的方式來創建數組例如下面的程序

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

上面的這個例子比較簡單,但是如果你有接觸過泛型數組,你便對他的復雜度有一定的了解,由於創建泛型數組比較復雜,所以在實際的應用過程中一般會選擇List的對泛型進行存儲,如果實在需要使用泛型數組,則需要注意數組的在運行時的類型,think in java這本書中,對泛型數組的處理通過四個小程序對其進行了比較完整的描述

程序一:這個程序主要說明了,在使用泛型數組中容易出現的問題,由於書中對於程序的說明比較詳細,所以只對程序做引用

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

程序二:這個程序主要是說明在程序的執行過程中,泛型數組的類型信息會被擦除,且在運行的過程中數組的類型有且僅有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. public ArrayOfGeneric2(int size) {
  17. ts = (T[]) new Object[size];
  18. }
  19. public T get(int index) {
  20. return ts[index];
  21. }
  22. public T[] rep() {
  23. return ts;
  24. }
  25. public void set(int index, T t) {
  26. ts[index] = t;
  27. }
  28. public static void main(String[] args) {
  29. ArrayOfGeneric2<String> aog2 = new ArrayOfGeneric2<String>(10);
  30. Object[] objs = aog2.rep();
  31. System.out.println(objs);
  32. /* will throw ClassCastException */
  33. // String[] strs = aog2.rep();
  34. // System.out.println(strs);
  35. }
  36. }

Copyright © Linux教程網 All Rights Reserved