迭代 hastable 键的枚举会引发 NoSuchElementException 错误

发布于 2024-11-30 17:33:27 字数 825 浏览 1 评论 0原文

我正在尝试使用枚举迭代哈希表中的键列表,但是我在列表中的最后一个键处不断收到 NoSuchElementException ?

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)

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();
}

Console output:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寻梦旅人 2024-12-07 17:33:27

您在循环中调用 nextElement() 两次。该调用将枚举指针向前移动。
您应该像下面这样修改您的代码:

while (e.hasMoreElements()) {
    String param = e.nextElement();
    System.out.println(param);
}

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);
}
删除→记忆 2024-12-07 17:33:27
for (String key : Collections.list(e))
    System.out.println(key);
for (String key : Collections.list(e))
    System.out.println(key);
差↓一点笑了 2024-12-07 17:33:27

每次调用 e.nextElement() 时,您都会从迭代器中获取下一个对象。您必须在每次调用之间检查e.hasMoreElement()


例子:

while(e.hasMoreElements()){
    String param = e.nextElement();
    System.out.println(param);
}

Every time you call e.nextElement() you take the next object from the iterator. You have to check e.hasMoreElement() between each call.


Example:

while(e.hasMoreElements()){
    String param = e.nextElement();
    System.out.println(param);
}
扭转时空 2024-12-07 17:33:27

您正在调用 nextElement 两次。像这样重构:

while(e.hasMoreElements()){


String param = (String) e.nextElement();
System.out.println(param);
}

You are calling nextElement twice. Refactor like this:

while(e.hasMoreElements()){


String param = (String) e.nextElement();
System.out.println(param);
}
季末如歌 2024-12-07 17:33:27

当您只保证可以调用一次而不会出现异常时,您会在循环内调用 e.nextElement() 两次。像这样重写循环:

while(e.hasMoreElements()){
  String param = e.nextElement();
  System.out.println(param);
}

You're calling e.nextElement() twice inside your loop when you're only guaranteed that you can call it once without an exception. Rewrite the loop like so:

while(e.hasMoreElements()){
  String param = e.nextElement();
  System.out.println(param);
}
愚人国度 2024-12-07 17:33:27

您在循环中调用 nextElement 两次。你应该只调用它一次,否则它会前进两次:

while(e.hasMoreElements()){
    String s = e.nextElement();
    System.out.println(s);
}

You're calling nextElement twice in the loop. You should call it only once, else it moves ahead twice:

while(e.hasMoreElements()){
    String s = e.nextElement();
    System.out.println(s);
}
同尘 2024-12-07 17:33:27

每次执行 e.nextElement() 时,您都会跳过一个。因此,您在循环的每次迭代中跳过两个元素。

Each time you do e.nextElement() you skip one. So you skip two elements in each iteration of your loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文