In the previous article, we have discussed about Java LinkedList sort() Method with Examples
In this article we are going to see the use of Java LinkedList set() method in Java along with suitable examples.
Java LinkedList set() Method with Examples
This java.util.LinkedList.set()
method is used to replace a present element to specific element in a specified position within the LinkedList.
It returns true if the index is present within the range or the size of the LinkedList else it throws IndexOutOfBoundException
. If the LinkedList is of one type, and we try to replace the existing element with another type then it shows ClassCastException
.
Syntax:
LinkedListName.set(int index, Object o)
Where,
LinkedListName
refers to the name of your LinkedList.Int index
refers to the specified index position of whose element is to be replaced with the new element.Object o
refers to the specific element that will replace the existing element of the LinkedList.
Let’s see different examples to understand it more clearly.
Method-1: Java LinkedList set() Method – Example with String Type LinkedList
Approach:
- Create a new LinkedList of type String.
- Add string elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Then replace the existing element with a new elenent in the LinkedList using
set()
method. - Print the new LinkedList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of string datatype LinkedList<String> l = new LinkedList<String>(); // Adding some elements to the LinkedList l.add("Hello"); l.add("this"); l.add("is"); l.add("an"); l.add("LinkedList"); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // setting old element to new element in the LinkedList using set(int index, Object o) l.set(4,"example of set() Method"); // Prints the new LinkedList elements System.out.println("The new elements of LinkedList are: "+l); } }
Output: The elements of LinkedList are: [Hello, this, is, an, LinkedList] The new elements of LinkedList are: [Hello, this, is, an, example of set() Method]
Method-2: Java LinkedList set() Method – Example with Integer Type LinkedList
Approach:
- Create a new LinkedList of type Integer.
- Add integer elements into the LinkedList using the add() method.
- Display the LinkedList elements.
- Then replace the existing element with a new element in the LinkedList using
set()
method. - Print the new LinkedList.
Program:
import java.util.*; public class Main { public static void main(String[] args) { // Create a LinkedList of Integer datatype LinkedList<Integer> l = new LinkedList<Integer>(); // Adding some elements to the LinkedList l.add(2); l.add(52); l.add(13); l.add(17); l.add(1); // Prints the LinkedList elements System.out.println("The elements of LinkedList are: "+l); // setting old element to new element from the LinkedList using set(int index, Object o) l.set(4,99); // Prints the new LinkedList elements System.out.println("The new elements of LinkedList are: "+l); } }
Output: The elements of LinkedList are: [2, 52, 13, 17, 1] The new elements of LinkedList are: [2, 52, 13, 17, 99]
Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.
Related Java Programs: