黑马程序员技术交流社区

标题: 关于Enumeration和Iterator的比较 [打印本页]

作者: Без_тебя    时间: 2014-6-14 21:43
标题: 关于Enumeration和Iterator的比较
关于Enumeration和Iterator的比较


Enumeration是jdk1.5之间人们常用遍历队列的方式,和现在常用的Iterator相似,Iterator是Enumeration的升级加强版


Enumeration有两个方法:hasMoreElements  和   nextElement


hasMoreElements返回的是布尔类型,说明是否还有元素
nextElement返回的是下一个元素




hasMoreElements类似   Iterator的  hasMore
nextElement类似  Iterator的  next


下面用一个例子来遍历下Vector
输出的是
        1
        3
        2
        5


public class demo5 {
        public static void main(String[] args) {
                Vector<Integer> ve = new Vector<Integer>();
               
                ve.add(1);
                ve.add(3);
                ve.add(2);
                ve.add(5);
               
                Enumeration<Integer> e = ve.elements();
               
                while(e.hasMoreElements()){
                        System.out.println(e.nextElement());
                }
        }
}



作者: Rodgers    时间: 2014-6-15 13:17
谢谢楼主的分享啦,已经收藏了
作者: Wokno    时间: 2014-6-15 19:19
路过看看。。。。。。。。。。。。
作者: 任黎明    时间: 2014-6-15 21:15
我来学习了!!!!!!!!!!!!!!
作者: gyw520gyw    时间: 2014-6-15 21:47
楼上的总在我前面




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2