我正在尝试使用枚举来迭代哈希表中的键列表,但是我在列表中的最后一个键处不断收到 NoSuchElementException ?
Hashtable<String, String> vars = new Hashtable<String, String>();
vars.put("POSTCODE","TU1 3ZU");
vars.put("EMAIL","[email protected] /cdn-cgi/l/email-protection");
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()
在你的循环中两次。该调用将枚举指针向前移动。
您应该像下面这样修改您的代码:
while (e.hasMoreElements()) {
String param = e.nextElement();
System.out.println(param);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)