传智播客-Java基础加强-day6

泛型,jdk1.5新特性,常在集合,反射中使用

没有使用泛型时,只要是对象,不管是什么类型的对象,都可以存储进同一个集合中.使用泛型集合,可以将一个集合中的元素限定为一个特定类型,集合中只能存储同一个类型的对象,这样更安全;并且当从集合获取一个对象时,编译器也可以知道这个对象的类型,不需要对对象进行强制类型转换,这更方便。在JDK1.5中,你可以按原来的方式将各种不同类型的数据装到一个集合中,但编译器会报告unchecked警告。

JDK 1.5的集合类希望你在定义集合时,明确表示你要向集合中装哪种类型的数据,无法加入指定类型以外的数据。

ArrayList<Integer> collection2=new ArrayList<Integer>();

collection2.add(1);

/*collection2.add(1L);

Collection2.add(“abc”);*///这两行代码编译时就报告了语法错误

Int i2=collection2.get(0);//不需要再进行类型转换

泛型是提供给javac编译器使用的,可以限定集合中的输入类型,让编译器挡住源程序中的非法输入,编译器编译带类型说明的集合时会去除掉“类型”信息,使程序运行效率不受影响,对于参数化的泛型类型,getClass();方法的返回值和原始类型完全一样。由于编译生成的字节码会去掉泛型的数据,例如,用反射得到集合,再调用其add方法即可。

?通配符可以匹配任何类型的对象,使用通配符可以引用其他各种参数化的类型,?通配符定义的变量方根用作引用,可以调用与通用与参数化无关的方法,不能调用与参数有关的方法。

限定通配符的上边界:

正确:Vector<? Extends Number> x = new Vector<Integer>();

错误:Vector<? Extends Number> x = new Vector<String>();//必须是Number的子类

限定通配符的下界:

正确:Vector<? super Integer> x=new Vector<Number>();

错误:Vector<? super Integer> x=new Vector<Byte>();//必须是Integer的父类 

似懂非懂,相关代码如下:

package cn.itcast.day2;

import java.lang.reflect.Constructor;

import java.util.ArrayList;

import java.util.Collection;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

 

public class GenericTest {

    /**

     * @param args

     * @throws NoSuchMethodException

     * @throws SecurityException

     */

    public static void main(String[] args) throws Exception{

       // TODO Auto-generated method stub

       ArrayList collection1=new ArrayList();

       collection1.add(1);

       collection1.add(1L);

       collection1.add("abc");

       //int i=(Integer)collection1.get(1);

      

        ArrayList<String> collection2=new ArrayList<String>();

       /*collection2.add(1);

       collection2.add(1L);*/

       collection2.add("abc");

       String element=collection2.get(0);

      

       Constructor<String> constructor1=String.class.getConstructor(StringBuffer.class);//获得String对象参数为StrngBuffer.class的构造方法

       String str2=constructor1.newInstance(new StringBuffer("abc"));//创建String实例对象,参数必须与获得的构造方法的参数类型相同

       System.out.println(str2.charAt(2));

      

       ArrayList<Integer> collection3=new ArrayList<Integer>();

       System.out.println(collection3.getClass()==collection2.getClass());

       collection3.getClass().getMethod("add", Object.class).invoke(collection3,"abc");

       System.out.println(collection3.get(0));

       printCollection(collection3);

      

       HashMap<String,Integer> maps=new HashMap<String,Integer>();//创建HashMap<String,Integer>类型集合

       maps.put("aaa",11);

       maps.put("bbb", 22);

       maps.put("ccc", 33);

       Set<Map.Entry<String,Integer>> entrySet=maps.entrySet();

       for(Map.Entry<String, Integer> entry: entrySet){

           System.out.println(entry.getKey() + ":" + entry.getValue());

       }

      

       add(1,2);

       add(1.5,2);

       add(1,"abc");

       swap(new String[]{"aaa","bbb","ccc"},1,2);

    }

   

    private static <T> T add(T x,T y){//仿C++泛型模板

       return null; 

    }

   

    private static <T> void swap(T[] a, int i,int j){//交换数组中两个数据的位置,此处的T只能泛指对象类型

       T tmp=a[i];

       a[i]=a[j];

       a[j]=tmp;

    }

   

    public static void printCollection(Collection<?> collection){

      

       System.out.println(collection.size());

       for(Object obj:collection){

           System.out.println(obj);

       }

      

    }

 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值