share

Monday, 18 May 2015

How to count number of element in LinkedList

Size() Method return number of element in the LinkdeList .

import java.util.LinkedList;

public class CountElement {
   
    public static void main(String[] args) {
       
        LinkedList<String> linkedList = new LinkedList<String>();
       
        linkedList.add("Mumbai");
        linkedList.add("Delhi");
        linkedList.add("Noida");
        linkedList.add("Gao");
        linkedList.add("Patna");
        //print linkedlist
        System.out.println("Before Remove Element Linklist is = "+linkedList);
       
        //Returns the number of elements in this list.
        System.out.println("number of element in linkedlist = "+linkedList.size());
       
    }

}


Output:

 Before Remove Element Linklist is = [Mumbai, Delhi, Noida, Gao, Patna]

 number of element in linkedlist = 5

No comments:

Post a Comment