size fo arraylist java code example

Example 1: ArrayList size() method in java

import java.util.ArrayList;
public class ArrayListSizeMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<Integer> al = new ArrayList<Integer>();
      int s = al.size();
      System.out.println("size of ArrayList: " + s);
      al.add(25);
      al.add(64);
      al.add(73);
      s = al.size();
      System.out.println("size of ArrayList after adding elements: " + s);
      al.clear();
      s = al.size();
      System.out.println("size of ArrayList after clearing elements: " + s);
   }
}

Example 2: ArrayList size() method in java

import java.util.ArrayList;
public class ArrayListSizeMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> al = new ArrayList<String>();
      int s = al.size();
      System.out.println("size of ArrayList: " + s);
      al.add("red");
      al.add("blue");
      al.add("green");
      s = al.size();
      System.out.println("size of ArrayList after adding elements: " + s);
      al.clear();
      s = al.size();
      System.out.println("size of ArrayList after clearing elements: " + s);
   }
}

Tags:

Java Example