Array List add at index Java Example

ArrayList is a dynamic array. It means it can grow as you add items in to it. If required, you can convert array in to ArrayList easily. There are time, the programmer wants to add an item at certain index. This is possible in ArrayList.

ArrayList has an overload add method that takes index. The signature would be ArrayList.add(index, Object).

When you add an object at an index, the item currently at the position gets shifted to accommodate this change. This is shown in diagram as below.

ArrayList with three items

Java ArrayList Before Add

The same ArrayList after, ‘NewItem’ has been added

Java ArrayList After Add

Here is code example for that.

If you all this function from a method, here will be the output:


Click Here to Leave a Comment Below

Leave a Reply: