Given a list of the integers and we have to replace it an element from specified index with a new element in java.
To replace an element in the list - we use List.set() method.
List.set() method
List.set() method replaces an existing element at given index with new element and returns the old object/element.
Syntax:
Object List.set(index, new_object);
Example:
Input:
List = [10, 20, 30, 40, 50]
Index =1
new_element = 1000
Output:
Updated list =[10, 1000, 30, 40, 50]
Program:
Output