Get the size and trim to size

ReturnMethodSummary
intsize()Returns the number of elements in this list.
voidtrimToSize()Trims the capacity of this ArrayList instance to be the list's current size.

  import java.util.ArrayList;

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

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

    al.trimToSize();

    System.out.println(al.size());
  }

}

The output:


5
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.