Jump to content

Array List question


Joseph

Recommended Posts

are you getting a ConcurrentModificationException?

 

if so what Swizzbeat suggested will work although I wouldn't recommend using concurrent collections as they can be slower

 

 

you're probably getting that error because you're trying to do something like this

 

for(Element e : list) {
    if(e == null) {
        continue;
    }
 
    e.function();
    list.remove(e); // <- will throw a ConcurrentModificationException!!
}

you cant modify a list that isnt concurrent while doing this type of loop. use a raw iterator instead like so

 

for(Iterator<Element> iterator = list.iterator(); iterator.hasNext();) {
     Element e = iterator.next();
 
     if(e == null) {
         continue;
     }
 
     e.function();
     iterator.remove(); // <- does the same thing but no exception will be thrown!
}

or like I stated before, you can use a CopyOnWriteArrayList or what the dude above suggested

Link to comment
Share on other sites

are you getting a ConcurrentModificationException?

 

if so what Swizzbeat suggested will work although I wouldn't recommend using concurrent collections as they can be slower

 

 

you're probably getting that error because you're trying to do something like this

for(Element e : list) {
    if(e == null) {
        continue;
    }
 
    e.function();
    list.remove(e); // <- will throw a ConcurrentModificationException!!
}

you cant modify a list that isnt concurrent while doing this type of loop. use a raw iterator instead like so

for(Iterator<Element> iterator = list.iterator(); iterator.hasNext();) {
     Element e = iterator.next();
 
     if(e == null) {
         continue;
     }
 
     e.function();
     iterator.remove(); // <- does the same thing but no exception will be thrown!
}

or like I stated before, you can use a CopyOnWriteArrayList or what the dude above suggested

I have a hunch this is what he's trying to do.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...