码迷,mamicode.com
首页 > 编程语言 > 详细

Iterator迭代器使用造成java.util.NoSuchElementException异常

时间:2017-09-21 09:40:50      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:异常   iterator   迭代器   

技术分享使用Iterator迭代器出现错误java.util.NoSuchElementException

错误写法:

Iterator<Map<String, Object>> it = list.iterator();

while(it.hasNext()){

if(it.next().get("listSrc") != null && "C".equals(it.next().get("listSrc").toString())){

it.remove();

}

}

错误原因:循环中不能用两次it.next()方法。

解决方法:it.next()取出的数据使用新的对象进行接收后再使用

技术分享正确写法:

Iterator<Map<String, Object>> it = list.iterator();

while(it.hasNext()){

Map<String, Object> iterator = it.next();  //使用新对象进行接收

if(iterator .get("listSrc") != null && "C".equals(iterator .get("listSrc").toString())){

it.remove();

}

}


本文出自 “飘动的云” 博客,转载请与作者联系!

Iterator迭代器使用造成java.util.NoSuchElementException异常

标签:异常   iterator   迭代器   

原文地址:http://piaodongdeyun.blog.51cto.com/5561905/1967225

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!