LinkedList add() Method in Java
-
boolean add(Object element): This method appends the specified element to the end of this list.
Syntax:boolean add(Object element)
Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended to end of the list.
Return Value: This method returns True after execution.
Example:
// Java code to illustrate boolean add(Object element)importjava.io.*;importjava.util.LinkedList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]) {// creating an empty LinkedListLinkedList list =newLinkedList();// use add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// Output the present listSystem.out.println("The list is:"+ list);// Adding new elements to the endlist.add("Last");list.add("Element");// printing the new listSystem.out.println("The new List is:"+ list);}}chevron_rightfilter_noneOutput:
The list is:[Geeks, for, Geeks, 10, 20] The new List is:[Geeks, for, Geeks, 10, 20, Last, Element]
-
void add(int index, Object element): This method inserts an element at a specified index in the list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).
Syntax:
void add(int index, Object element)
Parameters: This method accepts two parameters as described below.
- index: The index at which the specified element is to be inserted.
- element: The element which is needed to be inserted.
Return Value: This method does not return any value.
Exception:
Throws IndexOutOfBoundsException if the specified index is out of range (index size()).
Example:
// Java code to illustrate boolean add(Object element)importjava.io.*;importjava.util.LinkedList;publicclassLinkedListDemo {publicstaticvoidmain(String args[]) {// creating an empty LinkedListLinkedList list =newLinkedList();// use add() method to add elements in the listlist.add("Geeks");list.add("for");list.add("Geeks");list.add("10");list.add("20");// Output the present listSystem.out.println("The list is:"+ list);// Adding new elements to the endlist.add(2,"Hello");list.add(4,"End");// printing the new listSystem.out.println("The new List is:"+ list);}}chevron_rightfilter_noneOutput:
The list is:[Geeks, for, Geeks, 10, 20] The new List is:[Geeks, for, Hello, Geeks, End, 10, 20]
Recommended Posts:
- LinkedList set() Method in Java
- LinkedList contains() Method in Java
- LinkedList get() Method in Java
- LinkedList pop() Method in Java
- LinkedList clear() Method in Java
- LinkedList toArray() method in Java with Example
- LinkedList removeFirst() Method in Java
- LinkedList push() Method in Java
- LinkedList addLast() Method in Java
- LinkedList addAll() Method in Java
- LinkedList removeLastOccurrence() method in Java with Example
- LinkedList spliterator() method in Java
- LinkedList getFirst() Method in Java
- LinkedList getLast() Method in Java
- LinkedList removeFirstOccurrence() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

