aza*_*lut 11 lambda listiterator java-8
让我们来看看这个例子:
public class ListIteratorTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("element1");
list.add("element2");
list.add("element3");
list.add("element4");
ListIterator<String> iterator = list.listIterator();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这很好用:
// prints elements out, and then appropriately removes one after another
while (iterator.hasNext()){
System.out.println(iterator.next());
iterator.remove();
}
Run Code Online (Sandbox Code Playgroud)
虽然这会抛出IllegalStateException:
// throws IllegalStateException, why?
iterator.forEachRemaining(n -> {
System.out.println(n);
iterator.remove();
});
Run Code Online (Sandbox Code Playgroud)
我的问题很简短:为什么?
aio*_*obe 17
感谢@studro.请参阅下面的评论.
该API文档状态:
如果在迭代正在进行中以除调用此方法之外的任何方式修改基础集合,则未指定迭代器的行为.
看起来"未指定的行为"部分也适用于此内部迭代.
当然,该forEachRemaining行为等同的状态的文档
while (hasNext())
action.accept(next());
Run Code Online (Sandbox Code Playgroud)
如果action::accept确实调用iterator.remove()上面的代码段不应该抛出任何异常(如果remove是支持的操作).这可能是文档错误.