0

I am pretty new to android and I am having a difficult time using ArrayList .I used an arrayList in my code . Under some condition I update the arraylist value . But when I try to retrieve data from that array list I get java.util.Concurrent modification exception . 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 :

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 :

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 ++;
}
}
}
}
5
  • please post the logcat and tell me in what line the Exception was thrown Commented Mar 15, 2014 at 8:15
  • I already comment the error line that log cat point in above question . Commented Mar 15, 2014 at 8:17
  • ok, but it was hard to see Commented Mar 15, 2014 at 8:18
  • @ donfuxx I am sorry . This is log cat error : 03-15 03:44:40.409: E/AndroidRuntime(2720): Caused by: java.util.ConcurrentModificationException 03-15 03:44:40.409: E/AndroidRuntime(2720): at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360) 03-15 03:44:40.409: E/AndroidRuntime(2720): at com.example.barnyar.MainActivity.updateSubPlayer(MainActivity.java:1627) 03-15 03:44:40.409: E/AndroidRuntime(2720): ... 14 more Commented Mar 15, 2014 at 8:20
  • That's not relevant to the question, but you should consider 1. indenting that code, 2. abstracting over that if statements. They look quite the same (create a method for it) Commented Mar 15, 2014 at 8:21

3 Answers 3

1

In the first chunk of code, you've iterated of subPlayerAndCountForBackArrow and modified it (by calling 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.

Sign up to request clarification or add additional context in comments.

3 Comments

How should I correct it update the array list and retrieving data ?? :(
My first choice would be to try to avoid it using the same list as input (iterating and reading from it) and as output (removing elements from it). Take a short break, take a step back from the actual code and try to rethink what you actually want to achieve by using that modifying loop. There are certainly better ways to achieve this. Sadly it's quite hard to get what you're trying to achieve with that code (at least for me), so I can't point you to a better way directly because I don't understand in detail, what you're trying to do. Do you?
I am really sorry . There'll be a better way as you said . I will rethink about it .
0

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();
    }

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.