Java ArrayList replace at specific index
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To replace an element at a specific position in a Java ArrayList, use the set method. It is the right tool when the index already exists and you want to overwrite the old value without changing the size of the list.
The set Method
ArrayList exposes this method:
It performs three things:
- checks that the index is valid,
- writes the new element into that slot,
- returns the old element that used to be there.
A simple example looks like this:
Output:
The list still has three elements. Only the value at index 1 changed.
set Is Not the Same as add
This distinction matters a lot.
- '
set(index, value)replaces an existing element.' - '
add(value)appends a new element.' - '
add(index, value)inserts a new element and shifts later elements.'
If you use add(index, value) when you meant to replace, the list grows and the elements move, which may quietly break downstream logic.
Output:
That is insertion, not replacement.
The Index Must Already Exist
A common beginner error is trying to use set on an index that has not been created yet.
That throws IndexOutOfBoundsException because the list is empty. set cannot create new positions. The valid index range is from 0 to size() - 1.
If you are building the list for the first time, use add. Use set only after the list already contains a value at that position.
Safe Replacement With Dynamic Input
If the index comes from user input, a calculation, or another data source, validate it before replacing.
That guard is simple, but it prevents a large class of runtime errors.
Why the Return Value Can Be Useful
Many code examples ignore the return value from set, but it can be helpful for logging, comparisons, or update auditing.
If you need to know what changed, the old value is already available.
Performance Characteristics
For an ArrayList, replacement at a known valid index is efficient because the underlying structure is an array. Java can directly overwrite that slot, so the operation is generally constant time.
That efficiency is another reason to keep the distinction clear:
- '
setis direct replacement,' - '
add(index, value)requires shifting later elements,' - '
remove(index)shifts elements in the other direction.'
Using the right method makes intent clearer and performance more predictable.
Common Pitfalls
A common mistake is calling set on an empty list or on an index equal to size(). That fails because the slot does not exist yet.
Another issue is confusing replacement with insertion. If the code unexpectedly changes list length, check whether add(index, value) was used instead of set.
Developers also sometimes skip bounds checks when the index is derived from outside input. That makes a simple collection update depend on fragile assumptions.
Summary
- Use
ArrayList#setwhen you want to replace an existing element at a known index. - '
setdoes not grow the list or shift elements.' - The index must already be within the current bounds of the list.
- Use
addfor insertion andsetfor replacement. - The method returns the old value, which can be useful for logging or comparison.

