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

Leave a Reply

Your email address will not be published. Required fields are marked *

81 + = 87