Java Collection Tutorial - Java ArrayList.size()








Syntax

ArrayList.size() has the following syntax.

public int size()

Example

In the following code shows how to use ArrayList.size() method.

/* w ww  .j  a  v a2s .  c o m*/

import java.util.ArrayList;

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

      arrlist.add(15);
      arrlist.add(2);
      arrlist.add(2);
      arrlist.add(2);

      int retval = arrlist.size();
      System.out.println("Size of list = " + retval); 
   }
}

The code above generates the following result.