LinkedList()

LinkedList(): It is a default constructor of LinkedList class. It is used to create an empty LinkedList in java. It’s doesn’t provide the default capacity.

LinkedList<E> NameOfLinkedList =  new LinkedList<E>();

Where, E represents the type of elements in LinkedList.

LinkedList<String> listOfNames =  new LinkedList<String>();

Let say you want to create a LinkedList and add some names in LinkedList. You can create it by default constructor:

import java.util.LinkedList;
public class ExampleOfLinkedList 
{
	public static void main(String[] args) 
	{
		LinkedList<String> listOfNames = new LinkedList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
                for(String name : listOfNames)
		    System.out.println(name);
	}
}

Output: JAVA
GOAL
JAVA
GOAL

Leave a Comment