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(
    [17:21:28 WARN]:     at java.util.ArrayList$
    [17:21:28 WARN]:     at me.universe.xpfly.Method$
    [17:21:28 WARN]:     at
    [17:21:28 WARN]:     at org.bukkit.craftbukkit.v1_9_R1.scheduler.CraftScheduler.mainThreadHeartbeat(
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.D(
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.DedicatedServer.D(
    [17:21:28 WARN]:     at net.minecraft.server.v1_9_R1.MinecraftServer.C(
    [17:21:28 WARN]:     at
    [17:21:28 WARN]:     at
    Code (Text):
    public static void Removefall(Player p) {
             Bukkit.getScheduler().scheduleSyncDelayedTask(Main.plugin,new Runnable(){
                 public void run() {
                     if ((!Main.Fall.isEmpty()) && (Main.Fall.contains(p.getName()))) {
                             for (String s: Main.Fall) {
                                 if (s == p.getName()) {
  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


    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))
    • Informative Informative x 1
  6. Oh yeah almost forgot about the while loop :D