Java Collection Tutorial - Java LinkedList.size()








Syntax

LinkedList.size() has the following syntax.

public int size()

Example

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

/*from   w  w  w.  j  av  a 2  s .c om*/
import java.util.LinkedList;

public class Main {

   public static void main(String[] args) {
      LinkedList<String> list = new LinkedList<String>();

      // add some elements
      list.add("Hello");
      list.add("from java2s.com");

      // print the list
      System.out.println("LinkedList:" + list);

      // print the size of the list
      System.out.println("List size:" + list.size());

      // add 2 more elements
      list.add("HTML");
      list.add("CSS");

      // print the size of the list
      System.out.println("List size:" + list.size());
   }
}

The code above generates the following result.