Every time I try to call ArrayList.remove() on an element while in a for each loop, Java throws a ConcurrentModificationException. I need to remove an element in a for loop because in there is where I look through the objects to find objects which meet the criteria for removal.
Maybe using a lower level form of iteration will solve the problem? Removing elements from a list or array or whatever while iterating over it can be tricky depending on the underlying representation, so maybe Java doesn't know how to do it and you just need to get more specific so you can do it correctly yourself?
Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will thow this exception.
It is common for Iterators in Java to be invalid if the underlying data structure is changed. Since it is an arraylist you can use a regular for-loop and get/remove. Just remember to decrement the index after removing an element.
Name:
Anonymous2012-01-18 9:03
>>7
This has nothing to do with thread-safe problems. This is due to a moron who doesn't understand what the sugar syntactic interator for loop :
for(Renderable renderable:renderables){
>>1
Fuck off back to your AbstractCubicleFactoryFactory, ``faggot"
Name:
Anonymous2012-01-18 11:33
>>13
Go back to your own country you foreign piece of shit.
Name:
Anonymous2012-01-18 11:34
>>13 >>14
Actually I'm not a Java programmer, just an embedded systems designer who makes more than you ever will. The only reason I'm using java is because my friend asked me to help him write a game engine for android. I don't even code as a profession anymore, I design... I leave the coding to the slaves and only code when they fuck up big time.
>>13 is a toilet scrubber. Seriously. Based on some of his past posts, this idiot isn't capable of making a correct statement about programming. On top of that, his written communication skills suck.
Name:
Anonymous2012-01-18 11:52
>>16 >>18
both of you are morons who can't code properly. Go back to your fantasy lives where you dream that you're as successful as kodak master programmer.
Name:
Anonymous2012-01-18 11:57
>>19
Again, you're more than welcome to waddle over to ste #300 on 64rd and Hollis and tell me that.
Name:
Anonymous2012-01-18 11:59
>>19
Also you idiot, in case you haven't noticed, I have a different writing style than >>16. Again, you're stupid. And again, you have no possible future as a comoputer programmer.
>>20
If you has any good reading comphension you would notice that I stated that they are jealous of your enterprise java job and wish they were like you.
Please do not make my make my Kodak-san look like a moron.
Name:
Anonymous2012-01-18 13:35
Kodak is now a meme
Name:
Anonymous2012-01-18 13:40
>>1
you cannot remove elements of an array during a for each
use a manual while loop and SAVE where you are because removing an element during traversal fucks things up
Instead consider a proper Iterator or a basic for loop.
/* THE LOGICAL WAY */
Object o;
int i;
for(i=0;i<anus.size();++i){
/* BULLSHIT */
if(I_SHOULD_REMOVE_THIS_INDEX){
o = anus.remove(i);
--i;
/* BULLSHIT WITH O */
}
/* BULLSHIT */
}
>>31,33 http://www.docjar.com/html/api/java/util/ArrayList.java.html
Since Java arrays are fixed-size, they would eventually need to have a new array created and the objects copied into the new array using System.arraycopy. Internally, a Java ArrayList isn't much different than a C++ std::vector or a C array expanded using realloc (except for the bloated unneeded error handling).
>>36
An array and an ArrayList are two different things in Java. Also, an array in C *cannot* be expanded using realloc(). Holy cripes, please shut up and read a book.