Thursday 26 July 2012

Get Size of Java LinkedList Example


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


import java.util.LinkedList;

public class GetSizeOfJavaLinkedListExample {

 public static void main(String[] args) {

  // create an LinkedList object

  LinkedList lList = new LinkedList();

  // Add elements to the list

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

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

  // display elements of LinkedList

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

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

  int sizeOfList = lList.size();

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

 }
}


The output is:



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

No comments:

Post a Comment