Get the size of an arraylist after and before add and remove methods in Java

Description

The following code shows how to get the size of an arraylist after and before add and remove methods.

Example


/*ww w . j  a  v a 2  s.c o m*/
import java.util.ArrayList;

public class Main {
  public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    System.out.println("Initial size of al: " + al.size());

    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");
    al.add(1, "java2s.com");

    System.out.println("Size of al after additions: " + al.size());

    System.out.println("Contents of al: " + al);

    al.remove("F");
    al.remove(2);

    System.out.println("Size of al after deletions: " + al.size());
    System.out.println("Contents of al: " + al);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Collection »




Java ArrayList
Java Collection
Java Comparable
Java Comparator
Java HashMap
Java HashSet
Java Iterator
Java LinkedHashMap
Java LinkedHashSet
Java LinkedList
Java List
Java ListIterator
Java Map
Queue
Java Set
Stack
Java TreeMap
TreeSet