The set() method of the ArrayList class replaces the element at a specified index with a new element. Unlike add(), it does not change the size of the list; instead, it updates the existing element and returns the element that was previously stored at that position.
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList and
// add elements
ArrayList<Integer> n = new ArrayList<>();
n.add(1);
n.add(2);
n.add(3);
n.add(4);
n.add(5);
// Print the original ArrayList
System.out.println("Before operation: " + n);
// Replace element at
// index 3 with 9
int r = n.set(3, 9);
System.out.println("After operation: " + n);
System.out.println("Replaced element: " + r);
}
}
Output
Before operation: [1, 2, 3, 4, 5] After operation: [1, 2, 3, 9, 5] Replaced element: 4
Explanation: In this example, the set() method replaces the element at index 3 (4) with 9. The method returns the old element (4), while the ArrayList is updated to [1, 2, 3, 9, 5].
Syntax
public E set(int index, E element)
Parameters:
- index: Index of the element to replace.
- element: Element to be stored at the specified position.
Returns Value: It returns the element that was previously at the specified position.
Exception: IndexOutOfBoundsException: Thrown if the index is out of the valid range (index < 0 or index >= size).
Example: Handling an Invalid Index
// Handling IndexOutOfBoundsException
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
try {
// Create an ArrayList and add elements
ArrayList<Integer> n = new ArrayList<>();
n.add(1);
n.add(2);
n.add(3);
n.add(4);
n.add(5);
// Print the original ArrayList
System.out.println("Before operation: " + n);
// Attempt to replace an element at an invalid index
n.set(7, 9);
} catch (IndexOutOfBoundsException e) {
// Handle the exception
System.out.println("Exception: " + e);
}
}
}
Output
Before operation: [1, 2, 3, 4, 5] Exception: java.lang.IndexOutOfBoundsException: Index 7 out of bounds for length 5
Explanation: In this example, the set() method attempts to replace an element at index 7, which is outside the valid range of the ArrayList. As a result, an IndexOutOfBoundsException is thrown and handled using a try-catch block.
Example: Updating a Student Name
import java.util.ArrayList;
public class GFG {
public static void main(String[] args) {
ArrayList<String> students = new ArrayList<>();
students.add("Amit");
students.add("Rahul");
students.add("Priya");
students.set(1, "Rohan");
System.out.println(students);
}
}
Output
[Amit, Rohan, Priya]
Explanation: The element "Rahul" at index 1 is replaced with "Rohan". The size of the ArrayList remains unchanged.
set() Vs add()
set() | add() |
|---|---|
| Replaces an existing element. | Inserts a new element. |
| Does not change the size of the list. | Increases the size of the list. |
| Requires an existing valid index. | Can append an element or insert at a valid index. |
| Returns the replaced element. | Returns boolean (or void for add(int, E)). |
Advantages of set() Method
- Updates an existing element without changing the list size.
- Provides direct access to elements using their index.
- Returns the replaced element, making it easy to track updates.
- Executes in constant time for ArrayList.
- Simplifies element replacement without removing and adding elements manually.