Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this tutorial, we’ll learn how to insert an object in an ArrayList at a specific position.

2. Example

If we want to add an element to a specific position to an ArrayList we can use the add(int index, E element) method which is provided through the implementation of the interface of List<E>. This method let us add an element at a specific index.

It can also throw an IndexOutOfBoundsException in case the index is out of range (index < 0 or index > size()). This means we can’t use it to add an item at position 4 if we’ve only 4 items in an ArrayList since we start to count with 0. We’d have to use the standard add(E e) method instead here.

First, we’ll create a new ArrayList and add four elements to it:

List<Integer> integers = new ArrayList<>();
integers.add(5);
integers.add(6);
integers.add(7);
integers.add(8);
System.out.println(integers);

This will result into:

img_637528671724b

Now, if we add another element at index 1:

integers.add(1,9);
System.out.println(integers);

The ArrayList internally will first move the objects starting at the given index:

img_637528683cc07

This works cause ArrayList is a growable array that automatically resizes the capacity if needed:

img_63752869916a0

And then add the new item at the given index:

img_6375286ad6a38

Adding in a specific index will result in an operations performance of O(n/2) on average for an ArrayList. A LinkedList, for example, has a complexity of O(n/4) on average and O(1) if an index is 0. So we need to have a closer look at LinkedList if we heavily rely on adding elements at specific positions.

We can also see that the ordering of the elements isn’t correct anymore. When we manually add items at specific positions, this is something we often want to achieve. Otherwise, we could use integers.sort(Integer::compareTo) to sort the ArrayList again or implement our own Comparator.

3. Add an Object at the Beginning

Adding an element at the beginning of ArrayList can be useful in scenarios such as maintaining the order of task processing. Below, we discuss three different implementations for adding an element at the beginning of ArrayList.

3.1. Using the add() Method

As we discussed in the previous section about the add() method, we can also use it for adding an element to the beginning by using the index as 0:

integers.add(0, 1);

The time complexity of this approach is O(n), as it requires ArrayList to shift n elements to insert the element at the beginning.

3.2. Using the Collections.reverse() Method

We can use the Collections.reverse() method to reverse the input list, then add the element at the end using ArrayList.add() and at the end reverse() the ArrayList again. The net result of this sequence of operations is the addition of an element at the beginning of the ArrayList:

List<Integer> integers = new ArrayList<>();
integers.addAll(Arrays.asList(4,5,6,7));

Collections.reverse(integers);
integers.add(1);
Collections.reverse(integers);

The time complexity for Collections.reverse() is O(n), adding an element with add() is O(1), and reversing back again is O(n). Therefore, the total time complexity can be expressed as O(n) + O(1) + O(n)  = 2O(n) ≈ O(n).

4. Conclusion

In this article, we discussed the add(int index, E element) method so we can add a new element at a specific position to an ArrayList<E>. We’ve to take care to stay inside the index bounds of the ArrayList and make sure that we permit the correct object.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW