ArrayList - add, add element at specific index methods program







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







import java.util.ArrayList;
import java.util.List;
/**
* Copyright (c), AnkitMittal JavaMadeSoEasy.com
*/
public class ArrayListTest {
   public static void main(String args[]) {
          // creates array with initial capacity of 10.
          List<String> arrayList = new ArrayList<String>();
          System.out.println("--------add element to end of list");
          arrayList.add("ankit");
          arrayList.add("mittal");
          arrayList.add("javaMadeSoEasy");
          System.out.println("arrayList = "+arrayList);
          System.out.println("--------add element at 1st index i.e. after ankit");
          arrayList.add(1,"java");
          System.out.println("arrayList = "+arrayList);
         
   }
}
/*OUTPUT
--------add element to end of list
arrayList = [ankit, mittal, javaMadeSoEasy]
--------add element at 1st index i.e. after ankit
arrayList = [ankit, java, mittal, javaMadeSoEasy]
*/


RELATED LINKS>


ArrayList - remove, get, contains and set methods program


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


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


ArrayList - synchronizing using Collections.synchronizedList


ArrayList - making list unmodifiable using Collections.unmodifiableList


Vector - add, add element at specific index methods program


No comments:

Post a Comment