相关疑难解决方法(0)

Java:在for-each循环中删除List中的记录时出现异常

我有一个List,我想循环遍历该List并在某些条件下删除一些记录库.这就是我的工作

public void foo(List<Bar> recordList){
     for(Bar bar : recordList){
         if(bar.someCondition()){
              recordList.remove(bar);
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

此代码生成异常.如果我使用Iterator,那么它工作正常

public void foo(List<Bar> recordList){
     Iterator<Bar> iter = recordList.iterator();
     while(iter.hasNext()){
         Bar bar = iter.next();
         if(bar.someCondition()){
              iter.remove();
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

我猜我的问题:

  1. 为什么第一段代码不起作用?

  2. 如何使第一段代码工作?

java list arraylist

3
推荐指数
2
解决办法
1067
查看次数

Java中的并发修改异常

我在执行此代码时收到ConcurrentModificationException.我无法弄清楚它为什么会发生?

private void verifyBookingIfAvailable(ArrayList<Integer> list, int id) {

        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
                int value = iterator.next();
                if (value == id) {
                    int index = list.indexOf(id);

                    if (index != -1) {
                        list.remove(index);
                    }
                }
        }
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢.

java list arraylist concurrentmodification listiterator

3
推荐指数
1
解决办法
1万
查看次数

Java - 多次从ArrayList中删除时出错.(IllegalStateException异常)

我一直在谷歌搜索,似乎无法找到解决方案.我在这做错了什么?我的问题在标题中.这是我得到的例外:

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)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

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();
}
Run Code Online (Sandbox Code Playgroud)

java exception arraylist minecraft

3
推荐指数
1
解决办法
1万
查看次数

迭代Map时出现ConcurrentModificationException

我在下面有以下代码

Map<String, Integer> buyingItemEnumerationMap = this.toBuyItemEnumeration;
for (Entry<String, Integer> item : buyingItemEnumerationMap.entrySet()) {
   if(RandomEngine.boolChance(50)){ //will delete?
    buyingItemEnumerationMap.remove(item.getKey());
   }
   if(buyingItemEnumerationMap.size() == 1){
    break;
   }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在使用Android游戏,上面的代码以多线程方式运行.现在我有一个例外java.util.ConcurrentModificationException.我已经研究过如何解决这个问题,但似乎没有对我有所帮助.我在上面的代码上做的是随机删除一个条目.我怎样才能在那里实现它?

java iteration concurrency android map

3
推荐指数
2
解决办法
2620
查看次数

从迭代ArrayList内部删除对象的最有效方法

我正在尝试从迭代的ArrayList中删除一个对象但是不能从循环内部执行此操作,这是我目前所拥有的

        for(Pearl pearl : this.pearls){
            pearl.onDraw(canvas);

            if(fish.isCollide(pearl)){
                this.pearls.remove(pearl);
            }
        }
Run Code Online (Sandbox Code Playgroud)

如果ArrayList大于1,则上述代码不起作用.

我正在考虑将代码更改为以下内容,但更愿意知道是否有更简单的方法.

       List<Pearl> pearls_delete = new ArrayList<Pearl>();


       for(Pearl pearl : this.pearls){
            pearl.onDraw(canvas);

            if(fish.isCollide(pearl)){
                pearls_delete.add(pearl);
            }
        }

        this.pearls.removeAll(pearls_delete);
Run Code Online (Sandbox Code Playgroud)

java arrays arraylist

3
推荐指数
1
解决办法
76
查看次数

如何从HashMap中删除一个键?

我有这个哈希图

HashMap <Integer,Integer> H = new HashMap <Integer,Integer>();
Run Code Online (Sandbox Code Playgroud)

当我尝试从 HashMap 中删除密钥时,我收到此错误

**Exception in thread "main" java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:922)
at java.util.HashMap$KeyIterator.next(HashMap.java:956)
at Livre.montantTotal(Livre.java:42)** 
Run Code Online (Sandbox Code Playgroud)

这是我的代码

for (int e : H.keySet()){
    H.put(e, H.get(e)-1);
    if (H.get(e) == 0){
        H.remove(e);
    }
}   
Run Code Online (Sandbox Code Playgroud)

java hashmap

3
推荐指数
2
解决办法
1万
查看次数

了解concurrentModificationException和ArrayList的实现

我试着通过编写以下代码来重现ConcurrentModificationException:

List<String> last = new ArrayList<>();
last.add("a");
last.add("b");
for(String i : last){
    System.out.println(i);
    last.remove(i);
}
System.out.println(last);
Run Code Online (Sandbox Code Playgroud)

DEMO

既然,提到文件ArrayList

请注意,迭代器的故障快速行为无法得到保证,因为一般来说,在存在不同步的并发修改时,不可能做出任何硬性保证.

我预计在单线程程序中,这种检测是直截了当的.但程序打印出来了

a
[b]
Run Code Online (Sandbox Code Playgroud)

代替.为什么?

java iterator arraylist

3
推荐指数
2
解决办法
1735
查看次数

Java LinkedList并发修改异常

我为 LinkedList 编写了一个简单的代码,将数据添加到其中并对其进行迭代,
我导入了 LinkedList 和 Iterator 所需的包,代码中没有错误,

我收到以下异常 -

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
    at java.util.LinkedList$ListItr.next(Unknown Source)
    at collectionFramework.Demo.displayList(LinkedListDemo.java:38)
    at collectionFramework.LinkedListDemo.main(LinkedListDemo.java:13)
Run Code Online (Sandbox Code Playgroud)

这是我的代码 -

package collectionFramework;

import java.util.Iterator;
import java.util.LinkedList;

public class LinkedListDemo{

    public static void main(String[] args) {

        Demo obj = new Demo();

        obj.addToList();
        obj.displayList();
    }

}

class Demo{

    LinkedList<String> al;
    Iterator<String> itr;  

    Demo(){
        al = new LinkedList<String>();
        itr = al.iterator();
    }

    void addToList(){
          al.add("Aniruddha");  
          al.add("Hitesh");  
          al.add("Rahul");  
          al.add("Kshitij");  
    }

    void displayList(){

        while(itr.hasNext()){  
               System.out.println(itr.next());  
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java iterator linked-list

3
推荐指数
1
解决办法
2858
查看次数

'instanceof'不一致

嘿编程学生在这里.

我有一个JavaFX应用程序.我有一个窗格,我想从中删除所有矩形.这是代码:

public Pane yard = new Pane();
...
for(int i = 0; i < yard.getChildren().size(); i++)
{
    if(yard.getChildren().get(i) instanceof Rectangle)
    {
        yard.getChildren().remove(i);
    }
 }
Run Code Online (Sandbox Code Playgroud)

这有时很好.其他时候它根本无法删除任何东西.谢谢您的帮助.

java javafx

2
推荐指数
1
解决办法
130
查看次数

为什么在ArrayList上使用for-each循环不起作用?

偶然发现了这个代码,它抛出了一个ConcurrentModificationException

ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));

for (String s : list) {
    if (s.equals("a"))
        list.remove(s);
}
Run Code Online (Sandbox Code Playgroud)

如果你添加一个迭代器并使用while循环,代码工作正常:

ArrayList<String> list = new ArrayList<String>(Arrays.asList("a", "b", "c", "d"));
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
    String s = iter.next();

    if (s.equals("a")) {
        iter.remove();
    }
}
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么有必要Iterator<String>在这种情况下使用.是因为ArrayList没有某种迭代能力,尽管它是Collection子类

是否有必要使用while循环,或者您是否可以生成带有for循环的版本?

java exception arraylist

2
推荐指数
2
解决办法
1万
查看次数