使用Iterator引起的java.util.NoSuchElementException异常
2017-03-29 15:08
543 查看
使用Iterator引起的java.util.NoSuchElementException错误
今天在使用Iterator时写了如下代码:
运行发现报错:java.util.NoSuchElementException
检查发现是2次使用了Iterator.next() 方法,导致边界溢出
做如下修改解决
另要注意:当iterator中的elements个数为偶数是不会报错的
今天在使用Iterator时写了如下代码:
for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { HashMap hmap = new HashMap() ; hmap.put("file";, iter.next()); hmap.put("state";, (String)map.get(iter.next())); listItem.add(hmap); }
运行发现报错:java.util.NoSuchElementException
检查发现是2次使用了Iterator.next() 方法,导致边界溢出
做如下修改解决
另要注意:当iterator中的elements个数为偶数是不会报错的
for (Iterator iter = map.keySet().iterator(); iter.hasNext();) { HashMap hmap = new HashMap() ; String nextElement = (String)iter.next(); hmap.put("file";, nextElement); hmap.put("state";, (String)map.get(nextElement)); listItem.add(hmap); }
使用一次 iterator 的next()方法即可
相关文章推荐
- Iterator与java.util.NoSuchElementException异常
- JAVA Scanner 用法注意事项(scanner 使用错误:Exception in thread "main" java.util.NoSuchElementExceptionation )
- Sqoop导入数据到MySql时的java.util.NoSuchElementException异常
- java.util.NoSuchElementException异常
- Exception异常:java.util.NoSuchElementException: null
- Sqoop导入数据到MySql时的java.util.NoSuchElementException异常2
- [Nutch2.3]调用generate的时候出现java.util.NoSuchElementException异常
- Iterator错误:java.util.NoSuchElementException
- 【JAVA学习】Iterator迭代器导致的java.util.NoSuchElementException原因与解决方法
- Android编程:解决异常“java.util.NoSuchElementException”
- 迭代器(Iterator)的java.util.NoSuchElementException错误的改正
- java.util.NoSuchElementException: No mapping for class sun.awt.AppContext
- ibatis报错:Caused by: java.util.NoSuchElementException
- java.util.NoSuchElementException解决办法
- java.util.NoSuchElementException: Hashtable Enumerator
- java.util.NoSuchElementException: Timeout waiting for idle object
- java.util.NoSuchElementException解决办法
- 关于java.util.NoSuchElementException错误
- 不经意间犯得一个java.util.NoSuchElementException的错误
- java.util.NoSuchElementException: No mapping for class java.awt.Component