ArrayList - remove, get, contains and set methods program








ArrayList - remove, get, contains and set 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>();
          arrayList.add("ankit");
          arrayList.add("mittal");
          arrayList.add("javaMadeSoEasy");
          System.out.println("arrayList = "+arrayList);
         
          System.out.println("\n--------get element at 1st index i.e. mittal");
          System.out.println("element at 1st index = "+arrayList.get(1));  
         
          System.out.println("\n--------contains element  ");
          System.out.println("arrayList.contains = "+arrayList.contains("mittal"));
         
          System.out.println("\n--------Remove element at 1st index i.e. mittal");
          arrayList.remove(1);
          System.out.println("arrayList = "+arrayList);
          System.out.println("\n---------Remove ankit from list");
          arrayList.remove("ankit");
          System.out.println("arrayList = "+arrayList);
          System.out.println("\n---------Set newVal at 0th index");
          arrayList.set(0,"newVal"); //javaMadeSoEasy will be replace by newVal
          System.out.println("arrayList = "+arrayList);
         
         
         
         
         
   }
}
/*OUTPUT
arrayList = [ankit, mittal, javaMadeSoEasy]
--------get element at 1st index i.e. mittal
element at 1st index = mittal
--------contains element
arrayList.contains = true
--------Remove element at 1st index i.e. mittal
arrayList = [ankit, javaMadeSoEasy]
---------Remove ankit from list
arrayList = [javaMadeSoEasy]
---------Set newVal at 0th index
arrayList = [newVal]
*/


RELATED LINKS>

ArrayList - add, add element at specific index 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 - isEmpty, size and clear methods program


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