Java LinkedList size() Method Example

The size() method of java.util.LinkedList class will return us the actual size of the list(Count of number of elements present in the list).

Signature

public int size()

This method returns number of elements in this list.

Example

The following example shows the usage of java.util.LinkedList.size() method.

import java.util.LinkedList;

public class LinkedListExample {
	public static void main(String args[])
	{
		// create an empty LinkedList
		LinkedList l = new LinkedList();
		
		// use add() method to add elements to the list
		l.add("Element1");
		l.add("Element2");
		l.add("Element3");
		l.add("Element4");
		l.add("Element5");
		
		//Getting the size of the list
		System.out.println("Size of the List is : \""+l.size()+"\"");
			
		//Printing the elements of the list
		System.out.println("**Elements of the list **");
		for(String temp:l)
		{
			System.out.println(temp);
		}
	}
}

Output

Size of the List is : "5"
**Elements of the list **
Element1
Element2
Element3
Element4
Element5

Leave a Reply

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