3

我已经在谷歌上搜索了很多,似乎无法找到解决方案。我在这里做错了什么?我的问题在标题中。这是我得到的例外:

java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
at me.herp.derp.client.Config.updateItem(Config.java:24)
at me.herp.derp.client.Commands.parseCommand(Commands.java:23)
at me.herp.derp.client.ChatCommands.handleChatcommand(ChatCommands.java:29)
at net.minecraft.src.EntityClientPlayerMP.sendChatMessage(EntityClientPlayerMP.java:171)
at net.minecraft.src.GuiChat.keyTyped(GuiChat.java:104)
at net.minecraft.src.GuiScreen.handleKeyboardInput(GuiScreen.java:227)
at net.minecraft.src.GuiScreen.handleInput(GuiScreen.java:176)
at net.minecraft.client.Minecraft.runTick(Minecraft.java:1494)
at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:843)
at net.minecraft.client.Minecraft.run(Minecraft.java:768)
at java.lang.Thread.run(Unknown Source)

这是我的代码:

public static void updateItem(String item, String value)
{
    if (!hasValue(item))
    {
        addItem(item, value);
        return;
    }
    for (ConfigItem c : configItems)
    {
        if (c.ITEM.equals(item))
        {
            configItems.iterator().remove();
            break;
        }
    }
    ConfigFile.saveConfig();
}
4

2 回答 2

14

您的迭代器未正确初始化(next()未调用)。我建议这样编写代码:

Iterator<ConfigItem> it = configItems.iterator();
while(it.hasNext()){
    ConfigItem c = it.next();
    if (c.ITEM.equals(item))
    {
        it.remove();
        break;
    }
}
于 2012-11-24T08:56:06.423 回答
2

您只能在 Iterator.next() 之后调用 Iterator.remove()。试试这个:

Iterator<ConfigItem> i = configItems.iterator();
while(i.hasNext()) {
    ConfigItem next = i.next();
    if (next.equals(item))
    {
        i.remove();
        break;
    }
于 2012-11-24T08:54:58.450 回答