Joseph Posted March 16, 2014 Share Posted March 16, 2014 I'm trying to remove a string from an array list but I'm getting an exception. I forgot with one it was. I looked it up and they said I need to iterate it from the array list. But it doesn't seem like its what I want to do with it. Any suggestions? Link to comment Share on other sites More sharing options...
Swizzbeat Posted March 16, 2014 Share Posted March 16, 2014 Are you modifying it from another thread? ArrayList is not thread safe, but you can set it as so by doing: ArrayList<String> exmaple = (ArrayList<String>) Collections.synchronizedList(new ArrayList<String>()); Link to comment Share on other sites More sharing options...
Joseph Posted March 16, 2014 Author Share Posted March 16, 2014 i don't know about threads too much. That's will be the next thing ill learn. But i'm as modify the arraylist. ill try that out later. Ill elt you know how it goes Link to comment Share on other sites More sharing options...
lare96 Posted March 17, 2014 Share Posted March 17, 2014 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 More sharing options...
Sigma Posted March 24, 2014 Share Posted March 24, 2014 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 More sharing options...
Joseph Posted March 24, 2014 Author Share Posted March 24, 2014 I still haven't tried it yet Link to comment Share on other sites More sharing options...