Online Java Compiler By
JavaTpoint.com
import java.util.ArrayList; public class ArrayListAddExample5{ public static void main(String[] args){ List
list = new ArrayList<>(); list.add(0, "element1"); // ["element1"] list.add(1, "element2"); // ["element1", "element2"] list.add(3, "element2"); // throws IndexOutOfBoundsException } }
Output