ConcurrentModificationException

Discussion in 'Plugin Development' started by Connor2weirdness, Sep 1, 2015.

Thread Status:
Not open for further replies.
  1. Hallo! I've been having great trouble with a piece of code that loves nothing better than to irritate me with stacktraces, specifically ConcurrrentModificationException. I've looked around on Google but can't find a solution that applies to my situation.

    My old code was:
    Code:
    Collection<String> names = theArray // code obfuscated here
    for (String p : names) {
        names.add("awesome " + p);
    }
    However a solution I found said to make a 'clone' array and iterate through that. Which still didn't work.
    Code:
    Collection<String> names = theArray // code obfuscated here
    Collection<String> clone = names;
    for (String p : clone) {
        names.add("awesome " + p);
    }
    Neither of these worked sadly. Thanks to anyone that can help!
     
    Last edited: Sep 1, 2015
  2. Offline

    meguy26

    @Connor2weirdness
    Wait wait wait....
    why are you iterating through a list and adding all its values to itself?
     
  3. It's a clone. That was my second revision of the code after hearing making a duplicate of an array then looping through that would avoid the exception. Please read ;) just kidding.
     
  4. Offline

    Oxyorum

    @Connor2weirdness Your second example code will work with some changes, just have the second collection be empty and iterate through the first collection.

    Code:
    for(item : collection1){
      collection2.add("txt"+item);
    }
    
     
  5. Offline

    meguy26

    @Connor2weirdness
    Its not a clone, its a reassigning of the variable. To clone it, it has to be a Collection that supports cloning, ArrayList being one of them.

    Oh I see now, you want to take the collection of player names, and add awesome to the beginning of every name?
    You could do:
    Code:
    Collection<String> names = theArray;
    List<String> updatedNames = new ArrayList<String>();
    
    for(String str : names) {
        updatedNames.add("awesome ".concat(str));
    }
     
  6. Offline

    mine-care

  7. Offline

    AdobeGFX

    Kevin DiTraglia from stackoverflow had a solution that might work.. But it's for removing from the arraylist, but might be worth a try
    link to the thread here
     
  8. Offline

    stefvanschie

    @meguy26 Use an iterator to go over the keys/values.
     
  9. Offline

    mythbusterma

    @AdobeGFX

    Please no.

    Just use an Iterator. That's the correct way.
     
    eyamaz likes this.
  10. Offline

    meguy26

    @stefvanschie
    If I'm not mistaken, foreach uses an iterator.
    :p
    https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
    And according to google:
    "The foreach loop, added in Java 5 (also called the "enhanced for loop"), is equivalent to using a java.util.Iterator --it's syntactic sugar for the same thing. Therefore, when reading each element, one by one and in order, a foreach should always be chosen over an iterator, as it is more convenient and concise"
     
  11. Thanks a lot! Solution worked perfect.
     
  12. Offline

    stefvanschie

    @meguy26 Use a seperate iterator.
    Code:
    Iterator iterator = mylist.iterator();
    or
    Code:
    Iterator iterator = myhashmap.keySet().iterator();
    .
     
  13. Offline

    meguy26

    @stefvanschie
    But then wouldnt it be easier to do a foreach for the keySet.. :p
    But yeah, I get what you mean now
     
Thread Status:
Not open for further replies.

Share This Page