ArrayList set() method in Java

The set() method of the ArrayList class is used to set a particular element at the specified position. In this post, we are going to look at the set() method in detail.

  • Method declaration – public E set(int index, E element)
  • What does it do? It will help in replacing the element present at the specified position ( passed in the first argument ) in the ArrayList with the specified element ( passed in the second argument ). Here the indexing is 0-based, so, if you want to change the value present at the 3rd position of the ArrayList, then you will have to pass the index as 2 into the function’s argument.
  • What does it return? It will return the old element that was replaced by the new element
Code Example
public class Codekru {

	public static void main(String[] args) throws Exception {

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

		// adding elements into the list
		al.add("first");
		al.add("second");

		System.out.println("ArrayList contents before: " + al);

		System.out.println("Old value present in the list: " + al.set(1, "last")); // it will print the old element
																					// present at index 1

		System.out.println("ArrayList contents after: " + al);
	}
}

Output –

ArrayList contents before: [first, second]
Old value present in the list: second
ArrayList contents after: [first, last]
set() vs add() method

We have talked about the add() method in one of our previous posts. If you want to know about it, then, please have a look at that article. Now, let’s move on to the difference between the set() vs add() method.

  • add() method is used to a new element to the list while set() method can’t be used to add a new element, rather it is used to replace one element with another
  • add() method will increase the size of the list whenever used while after using the set() method, the size of the list will remain the same

The What If scenarios

What if we try to use the set() method with an index < 0 or an index >= size of the ArrayList?

In this scenario, it will throw IndexOutOfBoundsException.

public class Codekru {

	public static void main(String[] args) throws Exception {

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

		// adding elements into the list
		al.add("first");
		al.add("second");

		System.out.println("Size of the ArrayList: " + al.size());

		System.out.println("Old value present in the list: " + al.set(2, "last"));

	}
}

Output –

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 2 out of bounds for length 2
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.set(ArrayList.java:441)
What if we try to use the set() method on a null ArrayList?

Now, it will throw a NullPointerException.

public class Codekru {

	public static void main(String[] args) throws Exception {

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

		al = null;

		System.out.println("Old value present in the list: " + al.set(0, "last"));

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.set(int, Object)" because "al" is null

Please visit this link if you want to know more about the ArrayList class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.