Java LinkedList add() Method with Examples

In the previous article, we have discussed about Java LinkedList addAll() Method with Examples

In this article we are going to see the use of Java LinkedList add() method along with suitable examples.

Java LinkedList add() Method with Examples

This java.util.LinkedList.add() method adds/inserts an element to the LinkedList. It always returns true as the collection need a return value in the signature when an element is added.

If we don’t insert any element inside the add() method then it will give NullPointerException.

add(Object element)

This add(Object element) method will add the element at the next successive index position means in the last of LinkedList.

Syntax:

LinkedListName.add(Object element)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Object element refers to the element that will be added into the LinkedList.

Example-1: Java LinkedList add() Method – Example with String Type LinkedList

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Add another new element to the LinkedList using add( ).
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("Hello");
        l.add("this");
        l.add("is");
        l.add("a");
        l.add("Linked List");
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l);
        // Adding a new element to the LinkedList
        l.add("Thank you");
        // Prints the new LinkedList elements
        System.out.println("The new elements in the LinkedList are: "+l);    
    }
}
Output:

The elements in the LinkedList are: [Hello, this, is, a, Linked List]
The new elements in the LinkedList are: [Hello, this, is, a, Linked List, Thank you]

Example-2: Java LinkedList add() Method – Example with Integer Type LinkedList

  • Create a new LinkedList of type Integer.
  • Add Integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Add another new element to the LinkedList using add( ).
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l);
        // Adding a new element to the LinkedList
        l.add(2);
        // Prints the new LinkedList elements
        System.out.println("The new elements in the LinkedList are: "+l);
    }
}
Output:

The elements in the LinkedList are: [2, 52, 13, 17, 1]
The new elements in the LinkedList are: [2, 52, 13, 17, 1, 2]

add(int index, Object element)

This add(int index, Object element) method will add the element at the specified index position of the LinkedList.

Syntax:

LinkedListName.add(int index, Object element)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • Object element refers to the element that will be added into the LinkedList.
  • int index refers to the index value of the object element

Example-1: Java LinkedList add(int index, Object element) Method – Example with Object Type LinkedList

  • Create a new LinkedList of type Object.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Add a new String element to the LinkedList to a specific position using add(int index, Object o) method.
  • Print the new LinkedList.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Object> l = new LinkedList<Object>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // Adding a new element to the LinkedList
        l.add(3,"string");
        // Prints the new LinkedList elements
        System.out.println("The new elements of LinkedList are: "+l);
    }
}
Output:

The elements of LinkedList are: [2, 52, 13, 17, 1]
The new elements of LinkedList are: [2, 52, 13, string, 17, 1]

If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.

Related Java Programs: