Java Utility Library

Java LinkedList - addLast() Method



The java.util.LinkedList.addLast() method is used to append the specified element at the end of the list.

Syntax

public void addLast(E element)

Here, E is the type of element maintained by the container.


Parameters

element Specify element which need to be appended in the list.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.LinkedList.addLast() method is used to append the specified element at the end of the list.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a linkedlist
    LinkedList<Integer> MyList = new LinkedList<Integer>();

    //populating linkedlist using addLast method
    MyList.addLast(10);
    MyList.addLast(20);
    MyList.addLast(30);

    //printing linkedlist
    System.out.println("MyList contains: " + MyList);   
  }
}

The output of the above code will be:

MyList contains: [10, 20, 30]

❮ Java.util - LinkedList