Little problem to interate an array ConcurrentModificationException

Discussion in 'Spigot Plugin Development' started by dallabonalucas, May 25, 2015.

  1. Hi spigot comunity, i'm trying to interate over an array, i tryed with a interator, for, foreach but the same exception are launched

    Hi spigot comunity, i'm trying to iterate over an array, i tried with a iterator, for, foreach but the same exception are launched

    Thank You

    Code: http://pastebin.com/7fyRkaTK
    Error: http://pastebin.com/9Gyx3CUN
     
  2. Serializator

    Supporter

    Try to remove the player from the list outside the while loop.
     
  3. Try this:
    Code (Text):
    public static void removeHgPlayer(Player pl){
        boolean remove = false;
     
        while(inter.hasNext()){
            if(inter.next().getPlayer().equals(pl)){
                remove = true;
                break;
            }
        }
     
        if(remove){
            inter.remove();
            // Do other stuff
        }
    }
    The ConcurrentModificationException happens when you add/remove objects to/from an iterator. The reason why you can't do this is because the normal Collection classes don't utilize so-called Robust Iterators. If you want a robust iterator in Java you need to use CopyOnWriteArrayList or CopyOnWriteArraySet.

    For your situation, however, using those would be overkill.
     
  4. Thank you for your explanation, but it didn't work. CODE
     
  5. What do you mean by "didn't work"?
    Does it produce any errors?

    If it's not too much trouble, it'd help a lot if you posted all of your code.

    EDIT: I see you forgot to add some lines from my code.
    Copy my code exactly as it is written and it might work.