Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList addLast() method
Syntax and Examples


Introduction

The addLast() method in Java's LinkedList class is a fundamental tool for managing data. It allows you to append an element to the end of the list, effectively extending the list by one item. Think of it like adding a car to the back of a train – it becomes the last car on the line.

Syntax

public void addLast(E element)

Parameters

Parameter Description
element The element to be added to the end of the list. The type E signifies that this can be any object, but it must be consistent with the type of elements already in the list (or allowed by the list's generics).

Return Value

The addLast() method returns void. It doesn't produce a value; it simply modifies the linked list by adding the element at the end.

Examples

Adding a String to a LinkedList

This example demonstrates how to add a single string element to a LinkedList. We'll create an empty list, then use addLast() to append the string "Hello".

import java.util.LinkedList;

public class AddLastExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>();

        myList.addLast("Hello");

        System.out.println(myList); // Prints the contents of the list
    }
}
[Hello]

Explanation: We first create a LinkedList that stores strings. Then, we call addLast() and pass in the string "Hello". Finally, we print the list to see the added element.

Adding Multiple Elements

Here's an example showing how you can add several elements of different types (integers and strings) to a LinkedList. This showcases the flexibility of generics in Java.

import java.util.LinkedList;

public class AddLastMultipleExample {
    public static void main(String[] args) {
        LinkedList<Object> myList = new LinkedList<>(); // Using Object for mixed types

        myList.addLast(10);
        myList.addLast("World");
        myList.addLast(3.14);

        System.out.println(myList);
    }
}
[10, World, 3.14]

Explanation: We create a LinkedList that can hold any type of object (using the Object wrapper). We then use addLast() to add an integer, a string, and a double. The resulting list contains all three elements in the order they were added.

Adding Null Values

This example explores adding null values to a LinkedList. It's important to understand that LinkedList allows null elements (unlike some other collection types).

import java.util.LinkedList;

public class AddLastNullExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>();

        myList.addLast("First");
        myList.addLast(null);
        myList.addLast("Second");

        System.out.println(myList);
    }
}
[First, null, Second]

Explanation: We create a LinkedList of strings and add "First", then a null value, and finally "Second". The resulting list contains these three elements, including the null entry.

Adding to an Existing List

This example showcases adding an element to a LinkedList that already contains other elements. This is the most common use case for addLast() when working with existing lists.

import java.util.LinkedList;

public class AddLastExistingExample {
    public static void main(String[] args) {
        LinkedList<Integer> myList = new LinkedList<>();
        myList.addLast(1);
        myList.addLast(2);

        myList.addLast(3); // Adds 3 to the end

        System.out.println(myList);
    }
}
[1, 2, 3]

Explanation: We first create a LinkedList and add two integers (1 and 2). Then we use addLast() to append the integer 3. The output shows that 3 is added to the end of the list.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M