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

Java LinkedList set() Method

Java LinkedList set() Method

Java LinkedList Java LinkedList


LinkedList is an important class in the Java Collections Framework; it implements List interface.set() The method is LinkedList is a very useful method provided by the class for replacing the element at a specified position in the list.

Simply put,set() The method allows you to:

  1. Access a specific position in the list by index
  2. Replace the old element at that position with a new element
  3. Return the replaced old element

Method Syntax

set() The complete syntax of the method is as follows:

public

E set

(

int

index, E element

)

Parameter Description:

Return Value:


Usage examples

Let us understand through a complete code example set() the use of the method:

Example

import

java.util.LinkedList

;

public

class

LinkedListSetExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a LinkedList

        LinkedList

<
String
fruits
=
new
LinkedList
<>
(
)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

        fruits.

add

(

"Date"

)

;

       

       

System

.

out

.

println

(

"Original linked list: "

fruits

)

;

       

       

// Use the set() method to replace the second element

       

String

replaced

=

fruits.

set

(

1

,

"Blueberry"

)

;

       

       

System

.

out

.

println

(

"After replacement: "

replaced

)

;

       

System

.

out

.

println

(

"Modified list: "

fruits

)

;

   

}

}

Output result:

原始链表: [Apple, Banana, Cherry, Date]
被替换的元素: Banana
修改后的链表: [Apple, Blueberry, Cherry, Date]

Notes

Index out-of-bounds exception

If the specified index is out of the list range (index < 0 or index >= size()),set() method throws IndexOutOfBoundsException.

Example

// This will throw an exception because index 4 does not exist (the list has only 4 elements, index range is 0-3)

fruits.

set

(

4

,

"Fig"

)

;

Performance considerations

Because LinkedList is implemented based on a linked list,set() method needs to traverse the list to find the element at the specified position, so:

Does not allow null values

If both the LinkedList does not allow null elements (depending on the specific implementation); attempting to set a null value may throw NullPointerException.


Comparison with Other Methods

set() vs add()

set() vs get()


Practical Application Scenarios

set() The method is especially useful in the following scenarios:

  1. Update specific elements in the list
  2. Replace history records when implementing undo/redo features
  3. Modify intermediate-step data in algorithms
  4. Update object state in game development

Summary

LinkedList 's set() method is a simple but powerful tool that allows developers to precisely modify specific elements in the list. Understanding how this method works and its constraints will help you use LinkedList collections.

Java LinkedList Java LinkedList