The LinkedList.set(int index, E element)
method in Java is used to replace the element at the specified position in the LinkedList
with the specified element. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality.
Table of Contents
- Introduction
set
Method Syntax- Examples
- Replacing an Element at a Specific Position
- Handling IndexOutOfBoundsException
- Conclusion
Introduction
The LinkedList.set(int index, E element)
method is a member of the LinkedList
class in Java. It allows you to replace the element at a specified position in the LinkedList
with the specified element. This method is particularly useful when you need to update the value of an element at a specific position in the list.
set Method Syntax
The syntax for the set
method is as follows:
public E set(int index, E element)
- index: The index of the element to replace.
- element: The element to be stored at the specified position.
- Returns: The element previously at the specified position.
- Throws:
IndexOutOfBoundsException
if the index is out of range (index < 0 || index >= size()
).
Examples
Replacing an Element at a Specific Position
The set
method can be used to replace an element at a specified position in a LinkedList
.
Example
import java.util.LinkedList;
public class SetExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add("Apple");
list.add("Banana");
list.add("Orange");
// Replacing the element at index 1 with "Mango"
String replacedElement = list.set(1, "Mango");
// Printing the replaced element and the updated LinkedList
System.out.println("Replaced element: " + replacedElement);
System.out.println("Updated LinkedList: " + list);
}
}
Output:
Replaced element: Banana
Updated LinkedList: [Apple, Mango, Orange]
Handling IndexOutOfBoundsException
If the specified index is out of range, the set
method throws an IndexOutOfBoundsException
. It is important to handle this exception to avoid runtime errors.
Example
import java.util.LinkedList;
public class SetExceptionExample {
public static void main(String[] args) {
// Creating a LinkedList of Strings
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList
list.add("Apple");
list.add("Banana");
try {
// Attempting to replace an element at an out-of-range index
list.set(5, "Orange");
} catch (IndexOutOfBoundsException e) {
System.out.println("Exception: " + e.getMessage());
}
// Printing the LinkedList
System.out.println("LinkedList: " + list);
}
}
Output:
Exception: Index: 5, Size: 2
LinkedList: [Apple, Banana]
Conclusion
The LinkedList.set(int index, E element)
method in Java provides a way to replace the element at a specified position in a LinkedList
. By understanding how to use this method, you can efficiently update elements in your Java applications. Handling the potential IndexOutOfBoundsException
ensures your code remains robust and error-free when dealing with invalid indices.