Java LinkedList addLast() Method with Examples

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

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

Java LinkedList addLast() Method with Examples

This java.util.LinkedList.addLast() method adds/inserts an element to the LinkedList at the last index position.

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 addLast() method then it will give NullPointerException.

Syntax:

LinkedListName.addLast(Object element)

Where,

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

Let’s see different examples to understand it more clearly.

Example-1: Java LinkedList addLast() 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 addLast().
  • 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 element to the LinkedList in the last index position
        l.addLast("Thank you");
        // Prints the LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l);    
    }
}
Output:

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

Example-2: Java LinkedList addLast() 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 addLast().
  • 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 LinkedLists
        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 element to the LinkedList  in the last index
        l.addLast(99);
        // Prints the new LinkedList elements
        System.out.println("The elements in the LinkedList are: "+l);
    }
}
Output:

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

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Related Java Programs: