ArrayList set(int index E element) method in java
example on ArrayList set(int index, E element) method for IndexOutOfBoundsException
import java.util.ArrayList;
public class ArrayListSetMethodExample
{
public static void main(String[] args)
{
try
{
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(36);
al.add(23);
al.add(39);
al.add(69);
al.add(56);
System.out.println("ArrayList before using set() method: " + al);
// replace number at the index 7 with 25
System.out.println("Trying to replace the element at index greater than capacity: ");
int num = al.set(7, 25);
// printing modified ArrayList
System.out.println("ArrayList after using set() method: " + al);
// printing replaced element
System.out.println("Replaced number: " + num);
}
catch(IndexOutOfBoundsException ex)
{
System.out.println("Exception: " + ex);
}
}
}