简体   繁体   English

数组列表中的java.util.ConcurrentModificationException

[英]java.util.ConcurrentModificationException in Array List

I am pretty new to android and I am having a difficult time using ArrayList .I used an arrayList in my code . 我对android很陌生,使用ArrayList遇到了困难。我在代码中使用了arrayList。 Under some condition I update the arraylist value . 在某些情况下,我更新arraylist值。 But when I try to retrieve data from that array list I get java.util.Concurrent modification exception . 但是,当我尝试从该数组列表中检索数据时,出现java.util.Concurrent修改异常。 I tried to solve this problem as far as I can . 我尽力解决了这个问题。 But i really cannot solve . 但是我真的无法解决。 That's why I am asking here . 这就是为什么我在这里问。 Help me please . 请帮帮我 。

Declararation : 声明:

HashMap<String,List<Player>> subPlayerAndCountForBackArrow = new HashMap<String, List<Player>>();

Here is the code I used to update data in arrayList : 这是我用来更新arrayList中数据的代码:

Iterator<Player> iter = subPlayerAndCountForBackArrow.get(String.valueOf(imanopage)).iterator();
while   (iter.hasNext()) {
if (iter.next().getTag().toString().equals(old_Parent.getChildAt(0).getTag().toString())){       
iter.remove();
    }
}
subPlayerAndCountForBackArrow.get(String.valueOf(imanopage)).add((Player)isGoal.getChildAt(0));

This is the code I used to retrieve data from arrayList : 这是我用来从arrayList检索数据的代码:

if(v.getId()==R.id.imgRightArrow){
if(subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).size()>0){  //this is the error line 
Log.i("UpArrowUpArrowUpArrow", "UpArrow");
if(imanopage <= subPlayerAndCountForBackArrow.size()){
        subPlayer1.removeAllViews();
subPlayer2.removeAllViews();
subPlayer3.removeAllViews();
subPlayer4.removeAllViews();
for(int j = 0;j<subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).size();j++){

if(j == 0){
subPlayer1.addView(subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j));
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParamssubPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j).getLayoutParams();
     lp.addRule(RelativeLayout.CENTER_IN_PARENT);
     lp.width = 50;
     lp.height = 45;

} }

if(j == 1){
subPlayer2.addView(subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j));
      RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j).getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
lp.width = 50;
lp.height = 45;
}

if(j == 2){
subPlayer3.addView(subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j));
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j).getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
lp.width = 50;
lp.height = 45;
}

if(j == 3){
subPlayer4.addView(subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j));
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).get(j).getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
lp.width = 50;
lp.height = 45;
}
}
if(imanopage == subPlayerAndCountForBackArrow.size()){
}
      else{
imanopage ++;
}
}
}
}

In the first chunk of code, you've iterated of subPlayerAndCountForBackArrow and modified it (by calling iter.remove ). 在第一段代码中,您迭代了subPlayerAndCountForBackArrow 并对其进行了修改(通过调用iter.remove )。

Iterator<Player> iter = subPlayerAndCountForBackArrow.get(String.valueOf(imanopage)).iterator();
[...]
iter.remove();

A different chunk of code iterates (I assume separately) over the same list: 在同一列表上迭代(我分别假设)不同的代码块:

[...]
if(subPlayerAndCountForBackArrow.get(String.valueOf(imanopage+1)).size()>0){  //this is the error line 
[...]

You can't do that. 你不能那样做。 You can't iterate over a list you modify concurrently. 您无法遍历同时修改的列表。 That's what the exception tells you. 那就是异常告诉你的。

You can't remove items from a list while you iterate through it. 遍历列表时,无法从列表中删除项目。

The best solution I have seen is to create a second list, add the items you want to remove to the second list. 我见过的最好的解决方案是创建第二个列表,将要删除的项目添加到第二个列表。 Then iterate over your second list and for each item remove it from the first list. 然后遍历您的第二个列表,并为每个项目将其从第一个列表中删除。 When you're done, clear the second list. 完成后,清除第二个列表。 At this point there should be no references left to the items you want to remove. 在这一点上,应该没有要删除的项目的引用。

Here is some code, the first loop checks to see if the item should be removed and adds it to the remove list. 这是一些代码,第一个循环检查是否应删除该项目并将其添加到删除列表中。 The next loop removes them completely. 下一个循环将其完全删除。

    for (int i = 0; i < myList.size(); ++i) {
        Item item = myList.get(i);
        if (item.isValid()) {
            item.doStuff();
        } else {
            nulledItems.add(item);
        }
    }

    if (!nulledItems.isEmpty()) {
        for (Item item : nulledItems) {
            if (myList.contains(item)) {
                myList.remove(item);
            }
        }
        nulledItems.clear();
    }

This exception occurs when you try to iterate and modify the collection at the same time. 当您尝试同时迭代和修改集合时,会发生此异常。 So i would suggest to Take another List which holds the item to remove. 因此,我建议采用另一个包含要删除项目的列表。 And the iterate again to remove only. 并再次迭代仅删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM