CopyOnWriteArrayList - add, add element at specific index methods program







CopyOnWriteArrayList - add, add element at specific index methods program.




import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class CopyOnWriteArrayListTest {
   public static void main(String args[]) {
          List<String> copyOnWriteArrayList = new CopyOnWriteArrayList<String>();
          System.out.println("--------add element to end of list");
          copyOnWriteArrayList.add("ankit");
          copyOnWriteArrayList.add("mittal");
          copyOnWriteArrayList.add("javaMadeSoEasy");
          System.out.println("copyOnWriteArrayList = "+copyOnWriteArrayList);
          System.out.println("\n--------add element at 1st index i.e. after ankit");
          copyOnWriteArrayList.add(1,"java");
          System.out.println("copyOnWriteArrayList = "+copyOnWriteArrayList);
         
   }
}
/*OUTPUT
--------add element to end of list
copyOnWriteArrayList = [ankit, mittal, javaMadeSoEasy]
--------add element at 1st index i.e. after ankit
copyOnWriteArrayList = [ankit, java, mittal, javaMadeSoEasy]
*/



RELATED LINKS>


CopyOnWriteArrayList - iterate using iterator, listIterator, Enumeration and enhanced for loop


CopyOnWriteArrayList - fail-safe or fail-fast iteration using iterator, listIterator, Enumeration and enhanced for loop


CopyOnWriteArrayList - isEmpty, size and clear methods program



eEdit
Must read for you :