How To Set An Element At A Particular Index In LinkedList?
In this short tutorial, we will see how to set an element at a particular index in linkedList.
Introduction
The LinkedList class is present in the java.util
package.
LinkedList internally uses a doubly linkedlist for storing the variables. That’s why LinkedList is preferred when our frequent operation is insertion and deletion.
Whenever we want to set an element at a particular index in the LinkedList, we can use the set(int index, E element)
method.
Set An Element Using set(int index, E element)
Method
LinkedList class provides the set(int index, E element)
method to set an element at a particular index in the LinkedList. This method replaces the existing element with the specified element (element
) at the specified index (index
).
The method signature is given below.
1
public E set(int index,E element)
This method accepts two parameters as given below.
Parameters:
index
- index of the element to replace
element
- element to be stored at the specified position
This method returns the element previously at the specified position.
I have given a Java program to set an element at a particular index in LinkedList.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.LinkedList;
/**
* A Java program to set an element at the specified index
* using the set(int index, E element) method.
*
* @author coderolls.com
*/
public class LinkedListSet {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("IN");
linkedList.add("US");
linkedList.add("FR");
linkedList.add("JP");
linkedList.add("CN");
linkedList.add("RU");
System.out.println("LinkedList before setting an element at index 4:");
System.out.println(linkedList);
// replaces CN with BR
// returns the previously present element at index 4 i.e CN
String previousElement = linkedList.set(4, "BR");
System.out.println("\nLinkedList after setting an element BR at index 4:");
System.out.println(linkedList);
System.out.println("\nPreviously present element at index 4:");
System.out.println(previousElement);
}
}
Output:
1
2
3
4
5
6
7
8
LinkedList before setting an element at index 4:
[IN, US, FR, JP, CN, RU]
LinkedList after setting an element BR at index 4:
[IN, US, FR, JP, BR, RU]
Previously present element at index 4:
CN
Conclusion
1
public E set(int index,E element)
We can use the set(int index, E element)
method to set an element at a particular index in the LinkedList.
1
2
3
// replaces the element at index 4 with "BR"
// returns the previously present element at index 4
linkedList.set(4, "BR");
This method replaces the element at the specified position in this list with the specified element. —
The example Java programs given in the above tutorial can be found at this GitHub Repository.
Let me know your thoughts or suggestions on the topic discussed above in the comment section below.
Join Newsletter
Get the latest tutorials right in your inbox. We never spam!