Wednesday, 25 July 2012

Get Size of Java ArrayList Example


This Java Example shows how to get size of java ArrayList object using size method.

import java.util.ArrayList;

public class GetSizeOfJavaArrayListExample {

 public static void main(String[] args) {

  // create an ArrayList object

  ArrayList arrayList = new ArrayList();

  // Add elements to the list

  arrayList.add("1");
  arrayList.add("2");
  arrayList.add("3");

  System.out.println("ArrayList contains...");

  // display elements of ArrayList

  for (int index = 0; index < arrayList.size(); index++)
   System.out.println(arrayList.get(index));

  /**
   * size() Returns the number of elements in this list.
   **/

  int sizeOfList = arrayList.size();

  System.out.println("Size of the ArrayList= " + sizeOfList);

 }
}


The output is:



ArrayList contains...
1
2
3
Size of the ArrayList= 3

No comments:

Post a Comment