Java ArrayList addLast() Method

The ArrayList.addLast() method, introduced in Java 21, is used to add an element to the end of an ArrayList. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. Additionally, we will cover a real-world use case to illustrate its application.

Table of Contents

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

Introduction

The ArrayList.addLast() method is part of the ArrayList class in Java 21. It allows you to add an element to the end of the list directly, which simplifies the process of appending elements.

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.

How It Works

When you use the addLast(E element) method, the specified element is appended to the end of the ArrayList. Internally, it checks if there is enough capacity to add the new element. If not, it increases the capacity of the ArrayList. The method then adds the element to the next available position and increments the size of the list.

Examples

Adding an Element to the End of the List

The addLast method can be used to add an element to the end of the ArrayList.

Example

import java.util.ArrayList;
import java.util.List;

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

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

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

Output:

ArrayList after addLast: [Apple, Banana, Orange]

Handling Edge Cases

The addLast method should be used with care when the ArrayList is empty to ensure it handles the operation correctly.

Example

import java.util.ArrayList;
import java.util.List;

public class AddLastEdgeCaseExample {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();

        // Add an element to an empty list
        ((ArrayList<String>) list).addLast("Apple");

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

Output:

ArrayList after addLast to empty list: [Apple]

Real-World Use Case

Task Queue Management

In a task management system, tasks are often added to a queue for processing. The addLast() method can be used to ensure that new tasks are appended to the end of the queue.

Example

import java.util.ArrayList;
import java.util.List;

class Task {
    String name;
    boolean isProcessed;

    Task(String name) {
        this.name = name;
        this.isProcessed = false;
    }

    @Override
    public String toString() {
        return name + (isProcessed ? " (Processed)" : " (Pending)");
    }
}

public class TaskQueue {
    public static void main(String[] args) {
        List<Task> taskQueue = new ArrayList<>();
        taskQueue.add(new Task("Write report"));
        taskQueue.add(new Task("Prepare presentation"));

        // Add a new task to the end of the queue
        ((ArrayList<Task>) taskQueue).addLast(new Task("Fix critical bug"));

        // Display all tasks
        System.out.println("Task Queue:");
        taskQueue.forEach(task -> System.out.println(task));
    }
}

Output:

Task Queue:
Write report (Pending)
Prepare presentation (Pending)
Fix critical bug (Pending)

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. This method is particularly useful in real-world applications such as task queue management, where appending elements is necessary.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top