在ConcurrentModificationException的每个机器人 [英] ConcurrentModificationException at for each android

查看:154
本文介绍了在ConcurrentModificationException的每个机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递的ArrayList 的parseObject ,然后我把一个个的foreach 循环提取符合条件的项目,当用户对象不等于空。有哪些我面临两个问题。
1.如果我通过不同的数据传递到另一个列表,然后掠过我的适配器列表做code以下行,我得到随机数据以数字为例:如果项目#1的名称为MAC 那么它显示3项。

 的ArrayList<&的parseObject GT; checkRequestedNetArrayList =新的ArrayList<&的parseObject GT;();
requestedNetArrayList =(ArrayList的<&的parseObject GT;)对象;
MyResponsibilitesActivity.requestedNetArrayList = requestedNetArrayList;
adapterRequest =新GenericAdapter<&的parseObject GT;(
    getApplicationContext(),
    requestedNetArrayList,
    R.layout.requested_trust_net_list_item,
    requestedDataBinder);requestListView.setAdapter(adapterRequest);requestedNetArrayList =(ArrayList的<&的parseObject GT;)对象;对于(的parseObject对象:对象){
    的System.out.println(对象);
    object.getParseObject(用户);
    如果(object.has(用户)){        checkRequestedNetArrayList.add(对象);    }其他{
        checkRequestedNetArrayList.remove(对象);
    }}adapterRequest =新GenericAdapter<&的parseObject GT;(
    getApplicationContext(),
    checkRequestedNetArrayList,
    R.layout.requested_trust_net_list_item,
requestedDataBinder);requestListView.setAdapter(adapterRequest);

<醇开始=2>

  • 如果我这样做以下行code的只是直接给出的项目在同一个列表中,我得到了 java.util.ConcurrentModificationException

     的(对象的parseObject:对象){    如果(object.has(用户)){        requestedNetArrayList.add(对象);    }
    }
    其他{
        requestedNetArrayList.remove(对象);
    }adapterRequest =新GenericAdapter&LT;&的parseObject GT;(
        getApplicationContext(),
        requestedNetArrayList,
        R.layout.requested_trust_net_list_item,
        requestedDataBinder);requestListView.setAdapter(adapterRequest);

    }


  • 请帮我在这里。


    解决方案

    您无法从列表中,同时访问它删除元素。
    你必须使用迭代器。

    你在哪里都删除的对象,请使用 it.remove();

     的Iterator&LT;&的parseObject GT;它= objects.iterator();
    而(it.hasNext()){
        Object对象= it.next();
        //你的东西
        it.remove();
    }

    我想你可能想看看这篇文章了解深拷贝也。

    更新

    既然你想要的元素添加到列表中是不能直接用迭代器。现在,你所面临的问题,因为你是直接分配对象 requestedNetArrayList 的而不是做以下列方式:

     的ArrayList&LT;&的parseObject GT; requestedNetArrayList =新的ArrayList&LT;&GT;(对象);

    然后遍历对象,你现在做的,从删除或添加到
    requestedNetArrayList (你是pretty很多已经这样做)。

    I am passing Arraylist of ParseObject, and then i am putting one one foreach loop to extract the items with a condition when user object is not equals to null. There are two problems which i am facing. 1. If i am doing the following lines of code by passing different data to another list and then pass that list in my adapter, i am getting random data with numbers for example: If on item # 1 the name is "MAC" then it is showing in item 3.

    ArrayList<ParseObject> checkRequestedNetArrayList = new ArrayList<ParseObject>();
    requestedNetArrayList = (ArrayList<ParseObject>) objects;
    MyResponsibilitesActivity.requestedNetArrayList = requestedNetArrayList;
    adapterRequest = new GenericAdapter<ParseObject>(
        getApplicationContext(),
        requestedNetArrayList,
        R.layout.requested_trust_net_list_item,
        requestedDataBinder);
    
    requestListView.setAdapter(adapterRequest);
    
    requestedNetArrayList = (ArrayList<ParseObject>) objects;
    
    for(ParseObject object: objects){
        System.out.println(object);
        object.getParseObject("user");
        if(object.has("user")){
    
            checkRequestedNetArrayList.add(object);
    
        }else{
            checkRequestedNetArrayList.remove(object);
        }
    
    }
    
    adapterRequest = new GenericAdapter<ParseObject>(
        getApplicationContext(),
        checkRequestedNetArrayList,
        R.layout.requested_trust_net_list_item,
    requestedDataBinder);
    
    requestListView.setAdapter(adapterRequest);
    

    1. If i am doing the following line of code to just direct giving the items in the same list, i am getting the java.util.ConcurrentModificationException

      for(ParseObject object: objects){
      
          if(object.has("user")){
      
              requestedNetArrayList.add(object);
      
          }
      }
      else{
          requestedNetArrayList.remove(object);
      }
      
      adapterRequest = new GenericAdapter<ParseObject>(
          getApplicationContext(),
          requestedNetArrayList,
          R.layout.requested_trust_net_list_item,
          requestedDataBinder);
      
      requestListView.setAdapter(adapterRequest);
      

      }

    Please help me out here.

    解决方案

    You can not remove an element from list while accessing it. You have to use Iterator.

    Where ever you are removing the object, use it.remove();

    Iterator<ParseObject> it = objects.iterator();
    while(it.hasNext()){
        Object object = it.next();
        //your stuff
        it.remove();
    }
    

    I think you might want to check this article about deep copy also.

    UPDATE

    Since you want to add elements to the list it is not directly possible with iterator. Now you are facing problem because you are directly assigning objects to requestedNetArrayList instead of that do it in the following way :

        ArrayList<ParseObject> requestedNetArrayList = new ArrayList<>(objects);
    

    Then iterate over objects as you are doing now, and remove from or add to requestedNetArrayList (which you are pretty much already doing).

    这篇关于在ConcurrentModificationException的每个机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆