set(int index, E element) method

set(int index, E element): This int index e element method uses to replace the element at the specified position in LinkedList with the specified element. Its returns the element after replacement.

E set(int index, E element)

Where, E represents the type of elements in LinkedList.
index, index of the element to return.
throw, IndexOutOfBoundsException if index is invalid.

import java.util.LinkedList;
public class ExampleOfLinkedList
{
	public static void main(String[] args) 
        {
		LinkedList<String> listOfNames = new LinkedList<String>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("RAVI");
			
		listOfNames.set(1, "SITE");
		
		for(String name : listOfNames)
		   System.out.println(name);
	}
}

Output: JAVA
SITE
RAVI

Leave a Comment