迭代哈希表键的枚举时抛出NoSuchElementException错误。

7 浏览
0 Comments

迭代哈希表键的枚举时抛出NoSuchElementException错误。

我正在尝试使用枚举来迭代哈希表中的键列表,但是在列表的最后一个键处始终出现NoSuchElementException异常。

Hashtable vars = new Hashtable();
vars.put("POSTCODE","TU1 3ZU");
vars.put("EMAIL","[email protected]");
vars.put("DOB","02 Mar 1983");
Enumeration e = vars.keys();
while(e.hasMoreElements()){
System.out.println(e.nextElement());
String param = (String) e.nextElement();
}

控制台输出:

EMAIL

POSTCODE

Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator

at java.util.Hashtable$Enumerator.nextElement(Unknown Source)

at testscripts.webdrivertest.main(webdrivertest.java:47)

0