In this tutorial, we will learn about the Java ArrayList set() method, and learn how to use this method to set an element at given index, or say replace an element at given index in this ArrayList, with the help of examples.

Java ArrayList set() method

ArrayList set() replaces the element at the specified position in this ArrayList with the specified element.

Syntax

The syntax of set() method with index and element as arguments is

</>
Copy
ArrayList.set(int index, E element)

where

ParameterDescription
indexThe index of the element to replace in this ArrayList.
elementThe element to be stored at the specified index in this ArrayList.

Returns

The method returns the element that got replaced by the element at the given index.

1. set(index, element) – Set element at given index in ArrayList

In this example, we will initialize an ArrayList with four strings "a", "b", "c" and "d". We will replace the element at index 2 with the element "m". Since, the element at index 2 is "c", set() method sets the element at index 2 with element "m" and returns the element "c".

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		System.out.println("Original ArrayList    : " + arrayList);
		
		int index = 2;
		String element = "m";
		String result = arrayList.set(index, element);
		System.out.println("Return value from set() : " + result);
		System.out.println("ArrayList after set() : " + arrayList);
	}
}

Output

Original ArrayList    : [a, b, c, d]
Return value from set() : c
ArrayList after set() : [a, b, m, d]

2. set(index, element) – Index out of bounds

In this example, we will take an ArrayList of size 4 and try to set an element at index 6. As the given index 6 is out of bounds for the ArrayList with size 4, set() throws java.lang.IndexOutOfBoundsException.

Java Program

</>
Copy
import java.util.ArrayList;

public class Example {
	public static void main(String[] args) {
		ArrayList<String> arrayList = new ArrayList<String>();  
		arrayList.add("a");  
		arrayList.add("b");  
		arrayList.add("c"); 
		arrayList.add("d");
		System.out.println("Original ArrayList    : " + arrayList);
		
		int index = 6;
		String element = "m";
		String result = arrayList.set(index, element);
		System.out.println("Return value from set() : " + result);
		System.out.println("ArrayList after set() : " + arrayList);
	}
}

Output

Original ArrayList    : [a, b, c, d]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 6 out-of-bounds for length 4
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Unknown Source)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Unknown Source)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source)
	at java.base/java.util.Objects.checkIndex(Unknown Source)
	at java.base/java.util.ArrayList.set(Unknown Source)
	at Example.main(Example.java:14)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList set() method, and also learnt how to use this method with the help of examples.