读书人

器皿和泛型的简单介绍

发布时间: 2012-09-10 11:02:32 作者: rapoo

容器和泛型的简单介绍

JDK1.5泛型
在定义集合的时候同时定义集合中对象的类型
增强程序的可读性和稳定性
能把问题提前暴露在编译之前,让编译器发现,这样可以降低程序出错率

?

public class FanxingBianli {//泛型遍历public static void main(String[] args) {List<String> arrayList = new ArrayList<String>();arrayList.add("aaa");arrayList.add("bbb");arrayList.add("ccc");//使用JDK1.5遍历for(String temp : arrayList){System.out.println(temp);}//interface Iterator用来遍历CollectionSystem.out.println("use Iterator");Iterator it = arrayList.iterator();while(it.hasNext()){String temp = (String)it.next();System.out.println(temp);}System.out.println("use Iterator 泛型");Iterator<String> iterator = arrayList.iterator();while(iterator.hasNext()){//不需要将获得的对象转换为String对象String temp = iterator.next();System.out.println(temp);}}}

?

?

读书人网 >编程

热点推荐