Code example for CopyOnWriteArrayList

Methods: addAll, iterator, set

0
import libcore.java.util.SerializableTester; 
 
public final class CopyOnWriteArrayListTest extends TestCase {
 
    public void testIteratorAndNonStructuralChanges() { 
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
        list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
        Iterator<String> abcde = list.iterator();
        assertEquals("a", abcde.next());
        list.set(1, "B");
        assertEquals("b", abcde.next());
        assertEquals("c", abcde.next());
        assertEquals("d", abcde.next());
        assertEquals("e", abcde.next());
    } 
 
    /** 
     * The sub list throws on non-structural changes, even though that disagrees 
     * with the subList() documentation which suggests that only size-changing 
     * operations will trigger ConcurrentModificationException. 
     */