Skip to content

Tag: indexoutofboundsexception

Getting IndexOutOfBoundException

why does following main method gives IndexOutOfBoundException at list.add(1, 2)? Answer You can’t add an element at index 1 when it the ArrayList is empty. It starts at 0, or just use add. Per the ArrayList#add(int index, E element) javadoc, Throws: When size == 0, the index 1 is out of range.