Edit list Java
How to Replace a Element in Java ArrayList?To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters-the indexes of the element which has to be replaced and the new element. The index of an ArrayList is zero-based. So, to replace the first element, 0 should be the index passed as a parameter. Declaration: public Object set(int index, Object element)Return Value: The element which is at the specified index Exception Throws: IndexOutOfBoundsException Implementation: Here we will be proposing out 2 examples wherein one of them we will be setting the index within bound and in the other, we will be setting the index out of bounds. Example 1: Where Index is Within Bound Java
Output
[A, B, C, D]
[A, B, E, D]
Example 2: Where Index is Out of Bound Java
Output:
Article Tags :
Java Java Programs
Java-ArrayList Java-Collections Practice Tags :
Java Java-Collections
Read Full Article
|