Java LinkedList add(E e) Method Example

In this tutorial we will learn the usage of add(E e)  of java.util.LinkedList class. This method adds the specified element at the end of the LinkedList.

Signature

public boolean add(E e) 

This method appends the specified element to the end of this list.e is the element which will be appended and will return true on successful insertion.

Example

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

import java.util.LinkedList;

public class LinkedListAddExample 
{
	public static void main(String args[])
	{
		// create an empty LinkedList
		LinkedList<String> l = new LinkedList<String>();
		
		// use add() method to add elements to the list
		l.add("Element1");
		l.add("Element2");
		l.add("Element3");
		
		//Printing the elements of the list
		System.out.println("Elements of the list");
		for(String temp:l)
		{
			System.out.println(temp);
		}
	}
}

Output

Elements of the list
Element1
Element2
Element3

Leave a Reply

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