Java Iterator.remove() IllegalStateException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22361194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterator.remove() IllegalStateException
提问by Delfino
In the code below I have a try catch block that attempts to remove an element from a Vector, using Iterator. I've created my own class QueueExtendingVect
that extends Vector
and implements Iterator
.
在下面的代码中,我有一个 try catch 块,它尝试使用迭代器从 Vector 中删除一个元素。我创建了自己的类QueueExtendingVect
来扩展Vector
和实现Iterator
.
The variable qev1
is an instance of class QueueExtendingVect
. I've already added a few elements to this Vector as well.
变量qev1
是 class 的一个实例QueueExtendingVect
。我也已经向这个 Vector 添加了一些元素。
try
{
qev1.iterator().remove();
}
catch(UnsupportedOperationException e)
{
System.out.println("Calling Iterator.remove() and throwing exception.");
}
qev1.enqueue(ci);
qev2.enqueue(ci);
qcv1.enqueue(ci);
qcv2.enqueue(ci);
for (int i = 1; i < 5; i++)
{
if (i % 2 == 0)
{
qev1.enqueue(new CInteger(i+1));
qev2.enqueue(new CInteger(i+1));
qcv1.enqueue(new CInteger(i+1));
qcv2.enqueue(new CInteger(i+1));
}
else
{
qev1.enqueue(new Date(i*i));
qev2.enqueue(new Date(i*i));
qcv1.enqueue(new Date(i*i));
qcv2.enqueue(new Date(i*i));
}
}
In this code I add a few elements to the Vector qev1. The other variables are in other parts of the code.
在这段代码中,我向 Vector qev1 添加了一些元素。其他变量位于代码的其他部分。
However, when I run my program I get an IllegalStateException at runtime. I'm not sure what this means.
但是,当我运行我的程序时,我在运行时收到了 IllegalStateException。我不确定这意味着什么。
采纳答案by rgettman
You haven't called next()
on your Iterator
, so it's not referring to the first item yet. You can't remove the item that isn't specified yet.
您还没有调用next()
您的Iterator
,因此它还不是指第一项。您无法删除尚未指定的项目。
Call next()
to advance to the first item first, then call remove()
.
先调用next()
以前进到第一项,然后调用remove()
。
回答by Michu93
@rgettman answer is correct but to give you imagination.
@rgettman 答案是正确的,但要给您想象力。
Our collection: |el1| |el2| |el3|
我们的收藏:|el1| |el2| |el3|
when you call iterator.next()
it works this way:
当你这样称呼iterator.next()
它时:
|el1| iterator |el2| |el3|
|el1| 迭代器 |el2| |el3|
so it jumps over the element and return reference to the element which was jumped (|el1|). So if we called iterator.remove()
now, |el1| would be removed.
所以它跳过元素并返回对被跳转元素的引用(|el1|)。所以如果我们iterator.remove()
现在调用,|el1| 将被删除。
It's worth to add what @PedroBarros mentioned above - you can't call iterator.remove()
two times without iterator.next()
between them because IllegalStateException
would be thrown.
Also when you create two iterators (iterator1, iterator2) then calling:
值得添加上面提到的@PedroBarros - 你不能在没有它们之间调用iterator.remove()
两次iterator.next()
,因为IllegalStateException
会被抛出。同样,当您创建两个迭代器(iterator1、iterator2)然后调用:
iterator1.next();
iterator1.remove();
iterator2.next();
will throw ConcurrentModificationException because iterator2
checks that collection was modified.
将抛出 ConcurrentModificationException 因为iterator2
检查该集合已被修改。
回答by Deplover
It will also call this exeption, If you add something to the list in iterator and then after it not calling it.next() again but removing the item
它也会调用这个 exeption,如果你在迭代器中向列表中添加了一些东西,然后在它之后不再调用 it.next() 而是删除该项目