In this example we will learn how to add an element to a specified index of an Java ArrayList. To add an element to the specified in index of the arraylist, we will follow the below steps.
- First step is that we need to create a new ArrayList.
- Populate the arrayList with elements, using add(E e) API method of ArrayList.
- Use add(int index, E element) method of the ArrayList.
- The method inserts the specified the element at the specified position in this list, without overwriting the element previously at the specified index, instead shifts the existing element to the right.