【发布时间】:2014-06-18 18:21:47
【问题描述】:
我读到在迭代集合时删除元素的正确方法是这样(使用迭代器):
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(18);
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
itr.remove();
}
但是,我收到了Exception in thread "main" java.lang.IllegalStateException,但我不知道为什么。
有人可以帮我吗?
【问题讨论】:
-
阅读文档,它明确指出:
IllegalStateException - if the next method has not yet been called, or the remove method has already been called after the last call to the next method
标签: java collections iterator