首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

器皿和泛型的简单介绍

2012-09-10 
容器和泛型的简单介绍JDK1.5泛型在定义集合的时候同时定义集合中对象的类型增强程序的可读性和稳定性能把

容器和泛型的简单介绍

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);}}}

?

?

热点排行