Introduction
The set()
method in Java's LinkedList
class allows you to modify the element at a specific index within the list. Think of it like replacing an item on a shelf—you specify which slot (index) and what new item you want to put there.
Syntax
public E set(int index, E element)
Parameters
Parameter | Description |
---|---|
index |
The index of the element to be replaced. Must be a valid index within the list (0 <= index < size()). |
element |
The new element that will replace the existing element at the specified index. |
Return Value
The set()
method returns the original element that was present at the specified index before it was replaced.
Examples
Example 1: Basic Usage
This example demonstrates how to use the set()
method to change an existing element in a LinkedList. We'll create a LinkedList of Strings, insert some initial values, and then update one of those values using set()
.
import java.util.LinkedList;
public class SetExample {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("Apple");
myList.add("Banana");
myList.add("Cherry");
System.out.println("Original list: " + myList);
String originalValue = myList.set(1, "Grape"); // Replace Banana at index 1 with Grape
System.out.println("Modified list: " + myList);
System.out.println("Original value at index 1: " + originalValue);
}
}
Original list: [Apple, Banana, Cherry]
Modified list: [Apple, Grape, Cherry]
Original value at index 1: Banana
Explanation:
- We created a LinkedList called myList
and added three String elements.
- The first System.out.println()
displays the original list.
- The line String originalValue = myList.set(1, "Grape");
replaces the element at index 1 (which was "Banana") with "Grape". The replaced value ("Banana") is stored in the originalValue
variable.
- The second System.out.println()
shows the modified list.
- The final System.out.println()
displays the original value that was at index 1.
Example 2: Handling Index Out of Bounds
This example demonstrates what happens if you try to use an invalid index (an index outside the bounds of the LinkedList) with the set()
method. It will result in an IndexOutOfBoundsException
.
import java.util.LinkedList;
public class SetExample2 {
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
try {
Integer originalValue = numbers.set(5, 40); // Index 5 is out of bounds
System.out.println("Modified list: " + numbers);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: Index out of bounds - " + e.getMessage());
}
}
}
Error: Index out of bounds - Index: 5, Size: 3
Explanation:
- We created a LinkedList called numbers
and added three Integer elements.
- The code attempts to use the set()
method with an index of 5. Since the list only has indices 0, 1, and 2, this is out of bounds.
- Because we anticipated that there might be a problem, we wrapped the call to set()
in a try...catch
block. This catches the expected IndexOutOfBoundsException
.
- The catch block prints an error message indicating that the index was out of bounds.
Example 3: Replacing with Null
This example shows how to replace a value in the list with null. LinkedLists can store null elements, so this is a valid operation.
import java.util.LinkedList;
public class SetExample3 {
public static void main(String[] args) {
LinkedList<String> names = new LinkedList<>();
names.add("Alice");
names.add("Bob");
names.add("Charlie");
System.out.println("Original list: " + names);
String originalValue = names.set(0, null); // Replace Alice at index 0 with null
System.out.println("Modified list: " + names);
System.out.println("Original value at index 0: " + originalValue);
}
}
Original list: [Alice, Bob, Charlie]
Modified list: [null, Bob, Charlie]
Original value at index 0: Alice
Explanation:
- We created a LinkedList called names
and added three String elements.
- The line String originalValue = names.set(0, null);
replaces the element at index 0 (which was "Alice") with null
. The replaced value ("Alice") is stored in the originalValue
variable.
- The subsequent print statements show how the list has been modified and what the original value was.