import java.util.LinkedList; public class ExampleLinkedList { public static void main(String[] args) { LinkedListlist = new LinkedList<>(); list.add("X"); list.add("Y"); list.add("Z"); System.out.println(list); //prints [X, Y, Z] list.set(1, "A"); System.out.println(list); //prints [X, A, Z] } }
import java.util.LinkedList; public class ExampleLinkedList2 { public static void main(String[] args) { LinkedListThis example creates a new LinkedList, adds three elements to it, and then replaces the second element with a new value using the `set()` method. The output of the program shows the original and modified lists. The LinkedList class is part of the java.util package in the Java standard library.list = new LinkedList<>(); list.add(5); list.add(10); list.add(15); System.out.println(list); //prints [5, 10, 15] list.set(1, 7); System.out.println(list); //prints [5, 7, 15] } }