本文介绍了迭代通过枚举hastable键会抛出NoSuchElementException错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用枚举来遍历散列表中的键列表但是我一直在列表的最后一个键处获得NoSuchElementException?
I am trying to iterate through a list of keys from a hash table using enumeration however I keep getting a NoSuchElementException at the last key in list?
Hashtable<String, String> vars = new Hashtable<String, String>();
vars.put("POSTCODE","TU1 3ZU");
vars.put("EMAIL","[email protected]");
vars.put("DOB","02 Mar 1983");
Enumeration<String> 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)
推荐答案
你打电话给 nextElement()
两次环。此调用将枚举指针向前移动。
您应修改以下代码:
You call nextElement()
twice in your loop. This call moves the enumeration pointer forward. You should modify your code like the following:
while (e.hasMoreElements()) {
String param = e.nextElement();
System.out.println(param);
}
这篇关于迭代通过枚举hastable键会抛出NoSuchElementException错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!