String input from keyboard
Vector<String> myVector = new Vector<String>(someArray.length); //assume Vector is populated
Iterator<String> itr = myVector.iterator();
for loop begins
while(itr.hasNext() && itr.next().equals(input)){
itr.remove();
}
...
while(itr.hasNext() // is this the problem source?
run more code
for loop ends
when the current element equals to a string input
, i want to remove that element, otherwise continue iterating. i keep getting concurrent exceptions here.
what else should i do? should i be moving my itr.next elsewhere?
QUESTION: i want the logic such that if the current Vector element equals to target, i want it removed from the Vector. how can i do that?
I do not know why you are getting concurrent modification exceptions, because removing items through an iterator is legitimate: according to the documentation,
If the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a
ConcurrentModificationException
.
To answer your question about removing from the vector all elements that are equal to target, the simplest solution is to use Vector
's removeAll
method.
myVector.removeAll(Collections.singletonList(input));