Iterator.remove () IllegalStateException

In the code below, I have a try catch block that tries to remove an element from a vector using an Iterator. I created my own class QueueExtendingVectthat extends Vectorand implements Iterator.

The variable qev1is an instance of the class QueueExtendingVect. I have already added some elements to this 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 several elements to the qev1 vector. Other variables are in other parts of the code.

However, when I run my program, I get an IllegalStateException at runtime. I'm not sure what that means.

+7
source share
3 answers

next() Iterator, . , .

next(), , remove().

+27

@rgettman , .

: | el1 | | EL2 | | EL3 |

iterator.next(), :

| El1 | | el2 | | EL3 |

, (| el1 |). , iterator.remove() , | el1 | .

, @PedroBarros - iterator.remove() iterator.next() , IllegalStateException . , (iterator1, iterator2), :

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

ConcurrentModificationException, iterator2 , .

+1

, - , it.next() ,

+1

Source: https://habr.com/ru/post/1531416/


All Articles