Code example for CopyOnWriteArrayList

Methods: addAll, set, subList

0
     * 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. 
     */ 
    public void testSubListAndNonStructuralChanges() { 
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
        list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
        List<String> bcd = list.subList(1, 4);
        list.set(2, "C");
        try { 
            bcd.get(1);
            fail();
        } catch (ConcurrentModificationException expected) {
        } 
    } 
 
    public void testSubListAndStructuralChanges() { 
        CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<String>();
        list.addAll(Arrays.asList("a", "b", "c", "d", "e"));
        List<String> bcd = list.subList(1, 4);
        list.clear();