Add Element(s) at Specified Index to ArrayList in Java

The Java ArrayList class is part of the Collection framework. The ArrayList is an implementation of a resizable array data structure that automatically grows and shrinks when elements are added or removed in the runtime, whenever required,

The new elements are always added to the end of current arraylist, unless we specifically mention the index where we want to add the new elements.

ArrayList<String> arraylist = new ArrayList<>();

arraylist.add("apple");			// [apple]
arraylist.add("banana");		// [apple, banana]

//Adding a new element at index position 1
arraylist.add(1, "grapes");			// [apple, grapes, banana]

//Adding multiple elements element at index position 0
arraylist.add(0, Arrays.asList("date", "guava"));			// [date, guava, apple, grapes, banana]

Let us learn more in detail.

1. ArrayList add() and addAll() Methods

The ArrayList.add() method inserts the specified element at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices). Note that indices start from 0.

The add() does not return any value.

void ArrayList.add(index, itemToAdd);

Similarly, if we have to add multiple items to the arraylist, we can use the method addAll(), which takes another collection and add it the specified index location. It returns true if the items have been successfully, else returns false.

boolean ArrayList.addAll(index, collectionOfItems);

2. Examples of Adding elements at the Specified Index

Let us take an example of adding an item at the index position1.

ArrayList<String> namesList = new ArrayList<>(Arrays.asList("alex", "brian", "charles"));

namesList.add(1, "Lokesh");

System.out.println(namesList);  //[alex, Lokesh, brian, charles]

Similarly, we can add multiple items to the list by passing another List to the addAll() method.

In the following example, we add the new items at the start of the list. It moves all the existing items to their right.

ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c"));

list.addAll(0, List.of("1", "2", "3"));

System.out.println(list);  //[1, 2, 3, a, b, c]

3. Watch out for IndexOutOfBoundsException

If the argument index is out of bounds, the add() and addAll() methods throws IndexOutOfBoundsException.

Assertions.assertThrows(IndexOutOfBoundsException.class, () -> {

  namesList.add(10, "Lokesh");
});

4. Conclusion

The ArrayList class provides convenient methods to add elements at the specified index. These methods add the new elements and shift the current element as well as subsequent elements to the right.

We must take care of type safety and any invalid index position supplied to the methods. Failing to do so will result in exceptions.

Happy Learning !!

Source Code on Github

Comments

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode