In this program, we are adding 5 elements (100, 200, 300, 400 and 500) to the ArrayList using "add()" method of ArrayList class.
Further, we are adding two more elements which are string type; here we will add "Hello" at index 1, "World" at index 2 and "Hi there" at index 4.
Syntax of ArrayList.add() method
ArrayList.add(index, element);
Here,
- index - is the index of the element, where we have to add an element
- element - is the element (item), which has to be added
-
Consider the program
Output