Description
Program to add a new element to LinkedList using add(E e) method of LinkedList class.
Example
import java.util.LinkedList; class LinkedListAdd { public static void main(String[] args) { // create a LinkedList LinkedList<String> list = new LinkedList<String>(); // Adding elements to the LinkedList list.add("Harry"); list.add("Ajeet"); list.add("Tom"); list.add("Steve"); // Displaying LinkedList elements System.out.println("LinkedList elements: "+list); // Adding another element list.add("Kate"); // Displaying LinkedList elements after add(E e) System.out.println("LinkedList elements: "+list); } }
Output:
LinkedList elements: [Harry, Ajeet, Tom, Steve] LinkedList elements: [Harry, Ajeet, Tom, Steve, Kate]
LinkedList.add(E e) method
public boolean add(E e)
: Appends the specified element to the end of this list.
This method is equivalent to addLast(E). More at: Java LinkedList.add(E e)
Leave a Reply