ArrayList - isEmpty, size and clear methods program







ArrayList - isEmpty, size and clear 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("arrayList.isEmpty() = "+arrayList.isEmpty());
          System.out.println("\n--------clear list - remove all elements from list");
          arrayList.clear();
         
          System.out.println("arrayList.isEmpty() = "+arrayList.isEmpty());
          System.out.println("arrayList = "+arrayList);
         
   }
}
/*OUTPUT
--------add element to end of list
arrayList = [ankit, mittal, javaMadeSoEasy]
arrayList.isEmpty() = false
--------clear list - remove all elements from list
arrayList.isEmpty() = true
arrayList = []
*/


RELATED LINKS>

ArrayList - add, add element at specific index methods program


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


eEdit
Must read for you :