How to replace element in an ArrayList?
July 27, 2020 by
We have seen how to add and remove elements from ArrayList in java. But sometimes, there may be a situation when we want to replace the element in ArrayList. We can use set[int index, E element] to replace the element.
set[int index, E element]
The set[index, E element] method is used to set the specified element at the specified index value. is used to replace the element at the specified position in ArrayList with the specified element. It returns the element after replacement.
Where, E represents the type of elements in ArrayList .
index, index of the element to return.
throw, IndexOutOfBoundsException if index is invalid.
Output: JAVA
SITE
RAVI
Java ArrayList methods
Internal working of ArrayList in java