Attention! ArrayList additions and deletions do not mess with ...

Often you need to use the programming process to the collection, and we ArrayList is often used, but in a recent increase in deletions and there have been some problems, sharing the next record.

Two of the code points following the

List<String> arrayList1 = new ArrayList<String>();
arrayList1.add("1");
arrayList1.add("2");
for (String s : arrayList1) {
    if("1".equals(s)){
        arrayList1.remove(s);
    }}
    List<String> arrayList2 = new ArrayList<String>();
    arrayList2.add("2");arrayList2.add("1");
    for (String s : arrayList2) {
        if("1".equals(s)){
        arrayList2.remove(s);
    }
}

Program results are as follows:

arrayList1 the remove method is successful,
the remove method of operation arrayList2 throw ConcurrentModificationException exception.

We view the source code to analyze the reason for the exception
because the essence is to use the foreach iterator Iterator, all Collecation collection classes will implement the Iterable interface.
Find iterator ArrayList class () method

public Iterator<E> iterator() {
    return new Itr();
}

Essence iterator is first call hasNext () method determines the next memory element is not present, then use the next () method to remove an element

public boolean hasNext() {
    return cursor != size;
}

@SuppressWarnings("unchecked")
public E next() {
    checkForComodification();
    int i = cursor;
    if (i >= size)
        throw new NoSuchElementException();
    Object\[\] elementData = ArrayList.this.elementData;
    if (i >= elementData.length)
        throw new ConcurrentModificationException();
    cursor = i + 1;
    return (E) elementData\[lastRet = i\];
}

The above arraylist1 Why can remove it successfully? In fact, it is only a cycle, so successful.

Because the element 1 after it remove, it becomes a size-1, then the variable inside Itr cursor 0 to 1, 1 = 1 at this time, the end of the cycle, so successful.

Why arraylist2 remove failure? Because it is the second time cycle, but also remove a success, but the third judge next time cursor is not equal to a value of 2 results in the current size 1, so the next execution method, the most important came before remove ArrayList operation results in a value of adding 1 modCount then Itr expectedModCount class remains unchanged, it will throw an exception.

final void checkForComodification() {
    if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

Similarly available, due to the add operation will lead to modCount increment, it can not be deleted in foreach, add, modify elements in the ArrayList.

In this regard, we recommend using an iterator Iterator remove elements.

Iterator<String> ite = arrayList2.iterator();
while(ite.hasNext()) {
    if("1".equals(ite.next())) {
        ite.remove();
    }
}

If there is concurrent operation, also we need to be locked Iterator operations.

Author: struggling small programmers
https://www.toutiao.com/i6754322606561690116/

I recommended to my blog to read more:

1. the Java the JVM, collections, multithreading, new series of tutorials

2. the Spring MVC, the Boot the Spring, the Spring series of tutorials Cloud

3. Maven, Git, the Eclipse, IDEA Intellij Tool Tutorial Series

4. the Java, the back-end architecture, Alibaba and other manufacturers face new questions

Life is beautiful, see tomorrow ~

Guess you like

Origin www.cnblogs.com/javastack/p/12515934.html