简体   繁体   English

Iterator.remove() IllegalStateException

[英]Iterator.remove() IllegalStateException

In the code below I have a try catch block that attempts to remove an element from a Vector, using Iterator.在下面的代码中,我有一个 try catch 块,它尝试使用迭代器从 Vector 中删除一个元素。 I've created my own class QueueExtendingVect that extends Vector and implements Iterator .我创建了自己的类QueueExtendingVect来扩展Vector并实现Iterator

The variable qev1 is an instance of class QueueExtendingVect .变量qev1QueueExtendingVect类的一个实例。 I've already added a few elements to this Vector as well.我也已经向这个 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.在这段代码中,我向 Vector qev1 添加了一些元素。 The other variables are in other parts of the code.其他变量位于代码的其他部分。

However, when I run my program I get an IllegalStateException at runtime.但是,当我运行我的程序时,我在运行时收到了 IllegalStateException。 I'm not sure what this means.我不确定这意味着什么。

You haven't called next() on your Iterator , so it's not referring to the first item yet.您尚未在Iterator上调用next() ,因此它尚未指代第一项。 You can't remove the item that isn't specified yet.您无法删除尚未指定的项目。

Call next() to advance to the first item first, then call remove() .先调用next()以前进到第一项,然后调用remove()

@rgettman answer is correct but to give you imagination. @rgettman 答案是正确的,但要给您想象力。

Our collection: |el1|我们的收藏:|el1| |el2| |el2| |el3| |el3|

when you call iterator.next() it works this way:当您调用iterator.next()它的工作方式如下:

|el1| |el1| iterator |el2|迭代器 |el2| |el3| |el3|

so it jumps over the element and return reference to the element which was jumped (|el1|).所以它跳过元素并返回对被跳转元素的引用(|el1|)。 So if we called iterator.remove() now, |el1|所以如果我们现在调用iterator.remove() ,|el1| would be removed.将被删除。

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.这是值得添加什么@PedroBarros上面提到的-你不能叫iterator.remove()两次没有iterator.next()他们之间因为IllegalStateException会被抛出。 Also when you create two iterators (iterator1, iterator2) then calling:同样,当您创建两个迭代器(iterator1、iterator2)然后调用:

iterator1.next();
iterator1.remove();
iterator2.next();

will throw ConcurrentModificationException because iterator2 checks that collection was modified.将抛出 ConcurrentModificationException 因为iterator2检查集合已被修改。

它也会调用这个 exeption,如果你在迭代器中向列表中添加了一些东西,然后在它之后不再调用it.next()而是删除该项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM