Java中Enumeration接口的用法

Enumeration是java.util中的一个接口类,在Enumeration中封装了有关枚举数据集合的方法,与Iterator差不多,用来遍历集合中的元素  但是枚举Enumeration只提供了遍历Vector和Hashtable类型集合元素的功能,这种类型的集合对象通过调用elements()方法获取一个Enumeration对象  然后Enumeratino对象再调用以下方法来对集合中的元素进行遍历。

hasMoreElements():判断Enumeration对象中是否还有数据

nextElement():获取Enumeration对象中的下一个数据

实例如下:

Enumeration req = request.getParameterNames();
 while (req.hasMoreElements()) {
     Object obj = (Object) req.nextElement();
     if (obj.toString().trim().equals("LastPage")) {   
         System.out.println("LastPage \n");
     } else if (obj.toString().trim().equals("NextPage")) {
        System.out.println("NextPage");
     }
 }

猜你喜欢

转载自www.cnblogs.com/exmyth/p/10332249.html
今日推荐