How to add values to a list while iterating it [duplicate]

偶尔善良 提交于 2019-12-13 01:23:28

问题


I Have a scenario such like this

List<String> xxx = new ArrayList
for(String yyy : xxx){
   for(String zzz:xyxy){
     if(!zzz.equals(yyy)){
       xxx.add(zzz);
     }
   }
}

But i get java.util.ConcurrentModificationException: null exception.Can anyone help me solve this issue.? Can anyone give me alternate method to perform this ?


回答1:


Looking at the ArrayList API:

The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.

So you're going to need to explicitly get a ListIterator and use that to properly alter your ArrayList. Also, I don't believe you can use a for-each loop because the iterators in those are separate from your explicitly retrieved iterator. I think you'll have to use a while loop:

List<String> xxx = new ArrayList<>();
ListIterator<String> iterator = xxx.listIterator();
while (iterator.hasNext()) {
    String s = iterator.next();
    for (String zzz : xyxy) {
        if (!zzz.equals(s)) {
            iterator.add(zzz); //<-- Adding is done through the iterator
        }
    }
}



回答2:


Add them in another list and then add that list to xxx;

List<String> xxx = new ArrayList<>();
List<String> additional = new ArrayList<>();
for (String yyy : xxx) {
    for (String zzz : xyxy) {
        if (!zzz.equals(yyy)) {
            additional.add(zzz);
        }
    }
}
xxx.addAll(additional);



回答3:


Don't use a foreach loop, but use a normal loop like:

int originalSize=xxx.size();
for (int i=0;i<originalSize;i++) {
    xxx.add("New string appended at the end");
}

This allows you to iterate only on the items present in the list before you start to iterate, and it works if your only operation on the list is to add some new item at the end of it.




回答4:


To modify a list while iterating you can declare the arraylist as "java.util.concurrent.CopyOnWriteArrayList"

public static void main(String[] args) {
    List<String> xxx = new CopyOnWriteArrayList();
    xxx.add("3");
    xxx.add("4");

    List<String> xyxy = new ArrayList();
    xyxy.add("1");
    xyxy.add("2");
    xyxy.add("3");

    for (String yyy : xxx) {
        for (String zzz : xyxy) {
            if (!zzz.equals(yyy)) {
                xxx.add(zzz);
            }
        }
    }

    System.out.println(xxx);
}



回答5:


It means you cannot access and modify your collection (List) at the same time. To do so you must use an iterator. I dont understand what xyxy is in your code. but a possible iteration and adittion of element might look like this.

        List<String> xxx = new ArrayList<String>();

        xxx.add("hello");
        xxx.add("hi");
        xxx.add("hh");

        ListIterator<String> it = xxx.listIterator();
        while (it.hasNext()) {

            if (!it.next().equals("hi")) {
                it.remove();
                it.add("hi");

            }
        }

        System.out.println(xxx);
          }
      }


来源:https://stackoverflow.com/questions/23674666/how-to-add-values-to-a-list-while-iterating-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!