黑马 基础加强部分

来源:互联网 发布:python的set函数 编辑:程序博客网 时间:2024/05/16 01:57
 

android培训、java培训、期待与您交流!泛型
jdk1.5以前的集合类中存在的问题
ArrayList collection=new ArrayList();
collection.add(1);
collection.add(1L);
collection.add("abc");
int i=(Integer)arrayList.get(1);
编译要强制类型转换且运行时出错!
jdk1.5的集合类希望我们在定义集合时,明确表示要向集合中装哪种类型的数据,无法加入指定类型以外

的数据
ArrayList<String> collection1=new ArrayList<String>();
collection1.add("wangli");
/*
这两行代码在编译时就会报告语法错误
collection1.add(1);
collection1.add(1WL);
*/
//不需要再进行类型转换
String i=collection1.get(0);
泛型是提供给javac编译器使用的,可以限定集合中的输入类型,让编译器挡住原程序中的非法输入,编

译器编译带类型说明的集合时会去除掉“类型”信息,使程序运行效率不受影响,对于参数化的泛型类型

,getClass()方法的返回值和原始类型完全一样。由于编译生成的字节码会去掉泛型的类型信息,只要能

跳过编译器,就可以往某个泛型集合中加入其它类型的数据。
使用泛型集合,可以将一个集合中的元素限定为一个特定类型,集合中只能存储同一个类型的对象,这样

更安全;并且当从集合获取一个对象时,编译器也可以知道这个对象的类型,不需要对对象进行强制类型

转换,这样更方便。

具体实例:
 public static void main(String[] args) throws Exception{
  // TODO Auto-generated method stub
  ArrayList collection=new ArrayList();
  collection.add(1);
  collection.add(123L);
  collection.add("abc");
  //int i=(Integer)collection.get(1);
  
  ArrayList<String> collection1=new ArrayList<String>();
  collection1.add("wangli");
  String i=collection1.get(0);
  System.out.println(i);
  
  Constructor<String> constructor=String.class.getConstructor

(StringBuffer.class);
  String str=constructor.newInstance(new StringBuffer("wangli"));
  System.out.println(str.charAt(4));
  
  ArrayList<Integer> collection2=new ArrayList<Integer>();
  System.out.println(collection1.getClass()==collection2.getClass());
  
  collection2.getClass().getMethod("add", Object.class).invoke(collection2,

"wangli");
  System.out.println(collection2.get(0));

 }

ArrayList<E>类定义和ArrayList<Integer>类引用中涉及的一下术语:
1、整个称为ArrayList<E>泛型类型
2、ArrayList<E>中的E称为类型变量或类型参数
3、整个ArrayList<Integer>成为参数化的类型
4、ArrayList<Integer>中的Integer称为类型参数的实例或实际类型参数
5、ArrayList<Integer>中的<>念着typeof
6、ArrayList称为原始类型

参数化类型与原始类型的兼容性:
参数化类型可以引用一个原始类型的对象,编译报告警告,例如:Collection<String> c=new Vector();
原始类型可以引用一个参数化类型的对象,编译报告警告,例如:Collection c=new Vector<String>

();//原来的方法接受一个集合参数,新的类型也要能传进去

参数化类型不考虑类型参数的继承关系:
Vector<String> v=new Vector<Object>();//错误
Vector<String> v=new Vector();//正确
Vector<Object> v=new Vector<String>();//错误

在创建数组实例时,数组的元素不能使用参数化的类型,例如:
Vector<Integer> vectorList[] =new Vector<Integer>[10];//错误

Vector v=new Vector<String>();
Vector<Object> v1=v;//正确

泛型中的“?”通配符
使用?通配符可以引用其他各种参数化的类型,?通配符定义的变量主要用作引用,可以调用与参数化无关

的方法,不能调用与参数化有关的方法。
例:
 public static void main(String[] args){

  ArrayList<Integer> collection2=new ArrayList<Integer>();  
  collection2.getClass().getMethod("add", Object.class).invoke(collection2,

"wangli");
  pointCollection(collection2);

 }
 
 public static void pointCollection(Collection<?> collection){
  //collection.add("abc");//错误
  System.out.println(collection.size());
  for(Object obj:collection){
   System.out.println(obj);
  }
 }

限定通配符的上边界:
正确:Vector<? extends Number> x=new Vector<Integer>();
错误:Vector<? extends Number> x=new Vector<String>();
限定通配符的下边界:
正确:Vector<? super Integer> x=new Vector<Number>();
错误:Vector<? super Integer> x=new Vector<Byte>():
限定通配符总是包括自己。

泛型集合的综合案例:
 public static void main(String[] args){
  HashMap<String, Integer> maps=new HashMap<String, Integer>();
  maps.put("wangtao", 27);
  maps.put("wanggang", 25);
  maps.put("wangli", 23);
  Set<Map.Entry<String, Integer>> entrySet=maps.entrySet();
  for(Map.Entry<String, Integer> entry:entrySet){
   System.out.println(entry.getKey()+":"+entry.getValue());
  }
 }

定义泛型方法
java中的泛型类型类似于C++中的模板。但是这种相似性仅限于表面,java语言中的泛型基本上完全是在

编译器中实现,用于编译器执行类型检查和类型推断,然后生成普通的非泛型的字节码,这种实现技术称

为擦除(erasure)(编译器使用泛型类型信息保证类型安全,然后再生成字节码之前将其清除)。这是

因为扩展虚拟机指令集来支持泛型被认为是无法接受的,这会为java厂商升级其JVM造成难以逾越的障碍

。所以,java的泛型采用了可以完全在编译器中实现的擦除方法。
例如下面这两个方法,编译器会报错,它不认为是两个不同的参数类型,而认为是同一种参数类型。
private static void applyGeneric(Vector<String> v){}
private static void applyGeneric(Vector<Date> v){}

交换数组中的两个元素的位置的泛型方法语法为:
static <T> void swap(T[] a,int i,int j){
 T temp=a[i];
 a[i]=a[j];
 a[j]=temp;

用于放置泛型的类型参数的尖括号应出现在方法的其他所有修饰符之后和方法的返回类型之前,也就是紧

邻返回值之前。类型参数通常用单个大写字母表示

只有引用类型才能作为泛型方法的实际参数
swap(new String[]{"abc","wangli","itcast"},1,2);
swap(new int[]{1,2,3,4,5},3,4);//报错

除了在应用泛型时可以使用extends限定符,在定义泛型时也可以使用extends限定符,例如

Class.getAnnotation()方法的定义。并且可以用&来指定多个边界,如<V extends Serivalizable &

cloneable> void method(){}

普通方法、构造方法和静态方法中都可以使用泛型。
可以用类型变量表示异常,称为参数化的异常,可以用于方法的throws列表中,但是不能用于catch子句

中。
private static<T extends Exception> sayHello() throws T{
 try{
 }catch(Exception e){
  throw (T)e;
 }
}

在泛型中可以同时有多个类型参数,在定义它们的尖括号中用逗号分开,例如:
public static <K,V> V getValue(K key){return map.get(key);}

android培训、java培训、期待与您交流!
详细请查看:http://edu.csdn.net/heima
原创粉丝点击