手机版
你好,游客 登录 注册
背景:
阅读新闻

Java中泛型创建数组的总结

[日期:2012-02-16] 来源:Linux社区  作者:sun7545526 [字体:大 中 小]

程序三:主要说明在对象中通过用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.class, 10);  
  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. }  

泛型这一章节的内容从擦除开始,觉得都是非常的难懂,如果哪位同志有比较好的建议,希望能不惜指教!

linux
相关资讯       Java教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款