Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList addLast() Method

Java LinkedList addLast() Method

Java LinkedList Java LinkedList


addLast() The method is in Java's LinkedList is a commonly used method provided by the class for adding elements to the end of the list. This method is particularly useful for implementing queue (FIFO) data structures.


public void addLast(E e)

Method parameters

Parameters Type Description
e E The element to be added to the end of the list

Return Value

This method has no return value (void type).

Method Characteristics

  1. Time Complexity: O(1) - because LinkedList maintains pointers to the head and tail nodes, so elements can be added directly at the end
  2. Not thread-safe: This method is not thread-safe and requires external synchronization if used in a multi-threaded environment
  3. Allows null Values: Can add to the list null Element
  4. Capacity limit:LinkedList There is no capacity limit; elements can be added until memory is exhausted

Usage examples

Basic Usage

Example

import

java.util.LinkedList

;

public

class

LinkedListDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a LinkedList

        LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

       

       

// Use the addLast() method to add elements

        fruits.

addLast

(

"Apple"

)

;

        fruits.

addLast

(

"Banana"

)

;

        fruits.

addLast

(

"Cherry"

)

;

       

       

// Print the list

       

System

.

out

.

println

(

fruits

)

;

// Output: [Apple, Banana, Cherry]

   

}

}

Comparison with the add() method

Example

LinkedList

<
Integer
numbers
=
new
LinkedList
<>
(
)

;

// addLast() and add() are equivalent in LinkedList

numbers.

add

(

1

)

;

     

// Add at the end

numbers.

addLast

(

2

)

;

 

// Add at the end

System

.

out

.

println

(

numbers

)

;

// Output: [1, 2]


Exception cases

addLast() The method may throw the following exceptions:

  1. ClassCastException - If the class of the specified element does not allow it to be added to this list
  2. NullPointerException - If the list does not allow null elements and the specified element is null
  3. IllegalArgumentException - If some property of this element does not allow it to be added to this list

Practical Application Scenarios

  1. Implementing a queue:addLast() can be used to implement the enqueue operation of a queue
  2. Logging: This method can be used when recording events in chronological order
  3. History records: When implementing undo/redo functionality, a linked list can be used to store the operation history

Example

// Simple queue implementation example

LinkedList

<
String
queue
=
new
LinkedList
<>
(
)

;

// Enqueue operation

queue.

addLast

(

"Task1"

)

;

queue.

addLast

(

"Task2"

)

;

queue.

addLast

(

"Task3"

)

;

// Dequeue operation

String

firstTask

=

queue.

removeFirst

(

)

;

System

.

out

.

println

(

"Processing: "

firstTask

)

;


Notes

  1. For frequent random access operations,ArrayList may be more efficient than LinkedList more efficient
  2. In Java,LinkedList implements the Deque interface, so you can also use offerLast() method to achieve similar functionality
  3. If the list is empty,addLast() it will add the new element as the first and only element of the list

Summary

LinkedList 's addLast() The method is a simple but powerful tool for efficiently adding elements at the end of a list. Understanding how this method works and its applicable scenarios can help you make the right data structure choice when you need to frequently add elements at the end of a collection.

Java LinkedList Java LinkedList