Java ArrayList addLast() Method

The ArrayList.addLast() method, introduced in Java 21, is used to add an element to the end of the ArrayList. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. addLast Method Syntax
  3. Examples
    • Adding an Element to the End of the List
    • Handling Edge Cases
  4. Real-World Use Case
  5. Conclusion

Introduction

In Java 21, the ArrayList class has been enhanced with the addLast() method, which allows you to add an element to the end of the list. This method is particularly useful for appending elements in a straightforward manner.

addLast Method Syntax

The syntax for the addLast method is as follows:

public void addLast(E element)
  • element: The element to be added to the end of the list.

Examples

Adding an Element to the End of the List

The addLast method adds the specified element to the end of the ArrayList.

Example

import java.util.ArrayList;

public class AddLastExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");

        // Add an element to the end of the list
        list.addLast("Orange");

        System.out.println("ArrayList after addLast: " + list);
    }
}

Output:

ArrayList after addLast: [Apple, Banana, Orange]

Handling Edge Cases

Adding elements to the end of the list generally does not throw exceptions, but it's good to be aware of how the method integrates with other operations.

Example

import java.util.ArrayList;

public class AddLastWithOtherOperations {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("Apple");

        // Add an element to the end of the list
        list.addLast("Banana");
        list.addLast("Orange");

        // Remove the first element
        list.remove(0);

        // Add another element to the end
        list.addLast("Grapes");

        System.out.println("ArrayList after operations: " + list);
    }
}

Output:

ArrayList after operations: [Banana, Orange, Grapes]

Real-World Use Case

Queue Implementation

In a queue implementation, elements are added to the end of the list and removed from the front. The addLast() method is useful for enqueuing elements.

Example

import java.util.ArrayList;

class Queue<T> {
    private ArrayList<T> elements = new ArrayList<>();

    public void enqueue(T element) {
        elements.addLast(element);
    }

    public T dequeue() {
        if (elements.isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        return elements.remove(0);
    }

    public boolean isEmpty() {
        return elements.isEmpty();
    }

    @Override
    public String toString() {
        return elements.toString();
    }
}

public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new Queue<>();

        // Enqueue elements
        queue.enqueue("First");
        queue.enqueue("Second");
        queue.enqueue("Third");

        // Display queue
        System.out.println("Queue: " + queue);

        // Dequeue elements
        System.out.println("Dequeued: " + queue.dequeue());
        System.out.println("Queue after dequeue: " + queue);
    }
}

Output:

Queue: [First, Second, Third]
Dequeued: First
Queue after dequeue: [Second, Third]

Conclusion

The ArrayList.addLast() method in Java 21 provides a convenient way to add elements to the end of an ArrayList. By understanding how to use this method, you can efficiently manage the contents of your lists in Java applications, particularly in scenarios like queue implementations where appending elements is a common operation. The addLast() method offers a straightforward and effective solution for adding elements to the end of a list.

Comments