成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java中泛型創建數組的總結

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

在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. }   

程序三:主要說明在對象中通過用Object[]來保存數據,則生成對象是,可以對其持有的對象在T和object之間進行轉換,但是當設計到數組的轉換時,還是會報ClassCastException

  1. /**   
  2.  *    
  3.  * Initially, this doesn’t look very different compare with ArrayOfGeneric2.java , just that the cast has been moved.   
  4.  * Without the ©SuppressWarnings annotations, you will still get "unchecked" warnings. However, the internal   
  5.  * representation is now Object[] rather than T[]. When get( ) is called, it casts the object to T, which is in fact the   
  6.  * correct type, so that is safe. However, if you call rep( ) , it again attempts to cast the Object[] to a T[], which   
  7.  * is still incorrect, and produces a warning at compile time and an exception at run time. Thus there’s no way to   
  8.  * subvert the type of the underlying array, which can only be Object[]. The advantage of treating array internally as   
  9.  * Object[] instead of T[] is that it’s less likely that you’ll forget the runtime type of the array and accidentally   
  10.  * introduce a bug (although the majority, and perhaps all, of such bugs would be rapidly detected at run time)   
  11.  *    
  12.  *    
  13.  *    
  14.  * archive $ProjectName: $   
  15.  *    
  16.  * @author Admin   
  17.  *    
  18.  * @version $Revision: $ $Name: $   
  19.  */    
  20. public class ArrayOfGeneric3<T> {    
  21.     Object[] ts;    
  22.     
  23.     public ArrayOfGeneric3(int size) {    
  24.         ts = new Object[size];    
  25.     }    
  26.     
  27.     public T get(int index) {    
  28.         return (T) ts[index];    
  29.     }    
  30.     
  31.     public T[] rep() {    
  32.         return (T[]) ts;    
  33.     }    
  34.     
  35.     public void set(int index, T t) {    
  36.         ts[index] = t;    
  37.     }    
  38.     
  39.     public static void main(String[] args) {    
  40.         ArrayOfGeneric3<Integer> aog2 = new ArrayOfGeneric3<Integer>(10);    
  41.         Object[] objs = aog2.rep();    
  42.         for (int i = 0; i < 10; i++) {    
  43.             aog2.set(i, i);    
  44.             System.out.println(aog2.get(i));    
  45.         }    
  46.             Integer[] strs = aog2.rep();    
  47.             System.out.println(strs);    
  48.     }    
  49. }   

程序四:是對泛型數組相對而言比較***的解決方案

  1. /**   
  2.  *    
  3.  * The type token Class<T> is passed into the constructor in order to recover from the erasure, so that we can create   
  4.  * the actual type of array that we need, although the warning from the cast must be suppressed with @SuppressWarnings.   
  5.  * Once we do get the actual type, we can return it and get the desired results, as you see in main( ). The runtime type   
  6.  * of the array is the exact type T[].   
  7.  *    
  8.  * @author Admin   
  9.  *    
  10.  * @version $Revision: $ $Name: $   
  11.  */    
  12. public class ArrayOfGeneric4<T> {    
  13.     
  14.     T[] ts;    
  15.     
  16.     public ArrayOfGeneric4(Class<T> type, int size) {    
  17.         /* to solution array of generic key code! */    
  18.         ts = (T[]) Array.newInstance(type, 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.         ArrayOfGeneric4<Integer> aog2 = new ArrayOfGeneric4<Integer>(Integer.class10);    
  35.         Object[] objs = aog2.rep();    
  36.         for (int i = 0; i < 10; i++) {    
  37.             aog2.set(i, i);    
  38.             System.out.println(aog2.get(i));    
  39.         }    
  40.         try {    
  41.             Integer[] strs = aog2.rep();    
  42.             System.out.println("user Array.newInstance to create generci of array was successful!!!!! ");    
  43.         } catch (Exception ex) {    
  44.             ex.printStackTrace();    
  45.         }    
  46.     }    
  47. }   

泛型這一章節的內容從擦除開始,覺得都是非常的難懂,如果哪位同志有比較好的建議,希望能不惜指教!

原文鏈接:http://blog.csdn.net/sun7545526/article/details/7262486

【編輯推薦】

  1. Java Socket重要參數講解
  2. Java的輝煌還能延續多久?
  3. 深入理解Java對象序列化
  4. 對于Java類加載過程中的順序問題探究
  5. Java多線程之消費者生產者模式
責任編輯:林師授 來源: sun7545526的博客
相關推薦

2009-09-25 10:03:51

Java泛型

2009-08-25 14:03:17

2009-03-17 16:22:13

Java泛型接口

2009-08-24 15:38:21

C# 泛型數組

2009-10-10 09:27:42

Java泛型通用方法

2021-06-17 06:51:32

Java泛型Java編程

2017-03-06 16:51:52

Java泛型實現

2021-09-29 18:17:30

Go泛型語言

2024-04-23 08:23:36

TypeScript泛型Generics

2015-11-02 09:36:59

Javasuperextends

2021-12-30 19:34:15

Java泛型JDK

2009-06-16 11:32:00

Java泛型

2020-10-26 14:01:22

Java泛型

2011-06-03 08:49:54

Java

2009-06-11 17:31:27

Java泛型

2023-11-08 08:27:30

泛型Java

2021-07-09 06:11:37

Java泛型Object類型

2009-03-26 10:52:44

J2SE泛型集合

2009-08-24 16:39:19

C# 泛型應用

2021-03-01 07:34:42

Java泛型ArrayList
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品欧美一区二区三区 | 亚洲成人一区 | 国产一区二区视频免费在线观看 | 国产美女网站 | 懂色中文一区二区三区在线视频 | 欧美日韩a| 国产成人免费视频网站高清观看视频 | 国产亚洲欧美日韩精品一区二区三区 | 国产一区亚洲 | 午夜电影网 | 青青草精品 | 欧美精品一区二区三区在线播放 | 日韩av在线不卡 | 欧美视频二区 | 中文字幕日韩一区 | 中文字幕1区2区3区 亚洲国产成人精品女人久久久 | www.日本国产 | 久久久www成人免费精品 | 国产精品美女久久久久久久久久久 | 国产精品久久久久久久久久 | 99国内精品久久久久久久 | 久久久久久久久久久福利观看 | 精品国产欧美一区二区三区成人 | 久久久久久国产精品免费免费 | 欧美国产日韩精品 | 亚洲国产精品99久久久久久久久 | 99reav| 久久精品色欧美aⅴ一区二区 | 免费视频一区二区 | 国产精品免费观看 | 国产一区二区三区免费 | 爱综合| 欧美亚洲激情 | 国产欧美一区二区三区久久人妖 | 欧美三级网站 | 国产精品精品视频一区二区三区 | 中文字幕日韩欧美一区二区三区 | 网站黄色av| 精品国产一区二区在线 | 日日操夜夜操天天操 | 久久精品亚洲欧美日韩精品中文字幕 |