Replace a single-position element in ArrayList in Java

To replace an existing value at a specific position in ArrayList in java, use the set() of the ArrayList.

public set(int index, Object obj): this method returns the element that was deleted.

import java.util.ArrayList; 

public class ArrayListReplace {

public static void main(String[] args) {

// Create an ArrayList< Integer>
ArrayList< Integer> aList = new ArrayList< Integer> ();

//add digits to ArrayList
aList.add(1);
aList.add(2);
aList.add(3);
aList.add(4);

System.out.println("ArrayList ");
for(int e:aList)
System.out.println(e);

aList.set(2, 33);

System.out.println("ArrayList ");
for(int e:aList)
System.out.println(e);
}
}
Run:

ArrayList 
1
2
3
4
ArrayList
1
2
33
4