`
潇洒的拖鞋
  • 浏览: 9525 次
  • 性别: Icon_minigender_1
最近访客 更多访客>>
社区版块
存档分类
最新评论

黑马程序员----泛型学习注意点

 
阅读更多

---------------------- android培训java培训、期待与您交流! ----------------------

ArrayList<String>  list = new ArrayList <String>() ;
ArrayList <Integer> list2 = new ArrayList <Integer>();
//true;字节码相同,说明泛型是提供给编译器使用的,在runtime时没有泛型
System.out.println(list.getClass() == list2.getClass());

--------------------------------------------------------------------------------------------------------------
private static <T> void swap(T a[],int i,int j){//转换数组中任意2个元素的位置
        T temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

swap(new String []{"abc","xyz","jkl"}, 0, 2);
//  swap(new int[]{1,2,3}, 0, 2);//编译不通过,只有引用类型才能成为泛型的实际参数,int是基本类型

--------------------------------------------------------------------------------------------------------------------
private static <T> void copy1(Collection <T> dest,T src[]){//运用泛型向任意类型的集合中添加,相应类型的数组内容
        for(T s : src){
            dest.add(s);
        }
    }
   
    private static <T> void copy2(T []dest,T src[]){//相同类型的数组传值
       
    }

copy1(new Vector <String>(), new String [10]);
        copy2(new Date[10], new String [10]);//类型推断为Date和String的交集(Object)
//      copy1(new Vector <Date>(), new String [10]);//编译不通过,前面指定Date,泛型具有传递性,后面也应为Date






---------------------- android培训java培训、期待与您交流! ----------------------
详细请查看:http://edu.csdn.net/heima

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics