Solved Error arraylist loop

Discussion in 'Spigot Plugin Development' started by dentoffe, Jun 18, 2016.

  1. Hi,
    So I want to remove the player on the list but it throws out an exception :(
    I don't really understand what's I'm doing wrong and have tried a few option but they don't work.
    I don't just do Main.fall.remove(p.getname()) Because it is possible that he is in that list 2-3 times.

    Code (Text):
    [17:21:28 WARN]: java.util.ConcurrentModificationException
    [17:21:28 WARN]:     at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    [17:21:28 WARN]:     at java.util.ArrayList$Itr.next(ArrayList.java:851)
    [17:21:28 WARN]:     at me.universe.xpfly.Method$2.run(Method.java:71)
    [17:21:28 WARN]:     at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftTask.run(CraftTask.java:53)
    [17:21:28 WARN]:     at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:349)
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.D(MinecraftServer.java:681)
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.DedicatedServer.D(DedicatedServer.java:361)
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.C(MinecraftServer.java:630)
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.run(MinecraftServer.java:534)
    [17:21:28 WARN]:     at java.lang.Thread.run(Thread.java:745)
    Code (Text):
    public static void Removefall(Player p) {
             Bukkit.getScheduler().scheduleSyncDelayedTask(Main.plugin,new Runnable(){
                 
                 @Override
                 public void run() {
                     if ((!Main.Fall.isEmpty()) && (Main.Fall.contains(p.getName()))) {
                             for (String s: Main.Fall) {
                                 if (s == p.getName()) {
                                     Main.Fall.remove(p.getName());
                                 }
                             }
                     }
                 }
             },70);
        }
     
  2. It is impossible to remove an item from a list from within a foreach loop iterating said list. What you can do however is make a copy of the original arrayList, parse through the copy, but alter (remove the data) from the original arraylist. Also, you cannot compare Strings using ==, use .equals instead.
     
    • Useful Useful x 1
  3. _Ug

    _Ug

    Well, in your case you don't even have to loop if you only want to remove one player. You can just say if FlyingPlayers contains the player's name then remove the player's name from it.
     
    • Like Like x 1
  4. Because it is possible that he is in that list 2-3 times. Or will it remove it 3 times then?
    Thanks @Ferdz for helping me! :D
     
  5. To iterate on top of what @_Ug said, you could use a simple while loop
    Code (Text):
    while (list.contains(playerName))
      list.remove(playerName);
     
    • Informative Informative x 1
  6. Oh yeah almost forgot about the while loop :D