CodeGym /Java Blog /Java Collections /How to Replace an Element in Java ArrayList
Author
Alex Vypirailenko
Java Developer at Toshiba Global Commerce Solutions

How to Replace an Element in Java ArrayList

Published in the Java Collections group
Simple arrays in Java do not offer any method to update or replace an element. Nevertheless in ArrayList replace is pretty convenient to implement using the set method.

Method Header


arrayList.set(int index, dataType arrayListElement);

Parameters

The method takes 2 parameters.
  1. int index — The first one is the index of the element in ArrayList.

  2. dataType arrayListElement — The second parameter is the data to be replaced at the index specified.

Return Type

The method returns the same ArrayList element that is just replaced.

Example 1 - Replace element using the set() method


import java.util.ArrayList;
import java.util.List;

public class DriverClass {

	public static void main(String[] args) {

		List <String> weekDays = new ArrayList<>();
		weekDays.add("Monday");
		weekDays.add("Monday");
		weekDays.add("Wednesday");
		weekDays.add("Thursday");
		weekDays.add("Friday");
		weekDays.add("Saturday");
		weekDays.add("Sunday");
		
		System.out.println("Week Days (original) : " + weekDays + "\n");
		
		String replacingText = "Tuesday";
		String replacedText = weekDays.set(1, replacingText);
		
		System.out.println("Replacing Text:  " + replacingText);
		System.out.println("Replaced Text:  " + replacedText + "\n");
		System.out.println("Week Days (updated) :  " + weekDays);
	}
}

Output

Week Days (original) : [Monday, Monday, Wednesday, Thursday, Friday, Saturday, Sunday] Replacing Text: Tuesday Replaced Text: Monday Week Days (updated) : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

Explanation

In the snippet above, weekdays are added in an array list originally. However, Monday is added twice and Tuesday is missing. So, we replace it by Tuesday at the 1st index. This is done by using the set() method. Where index “1” and replacing text i-e “Tuesday” is passed. Later, we print out the ArrayList on the console to see the updates.

Example 2


import java.util.ArrayList;
import java.util.List;

public class DriverClass1 {

	public static void main(String[] args) {

		List<Integer> dieRoll = new ArrayList<>();

		dieRoll.add(0);
		dieRoll.add(1);
		dieRoll.add(2);
		dieRoll.add(3);
		dieRoll.add(4);
		dieRoll.add(5);

		System.out.println("Die Roll (original) : " + dieRoll + "\n");
		
		dieRoll.set(0, 1);
		dieRoll.set(1, 2);
		dieRoll.set(2, 3);
		dieRoll.set(3, 4);
		dieRoll.set(4, 5);
		dieRoll.set(5, 6);

		System.out.println("Die Roll (updated) :  " + dieRoll);
	}
}

Output

Die Roll (original) : [0, 1, 2, 3, 4, 5] Die Roll (updated) : [1, 2, 3, 4, 5, 6]

Conclusion

By now you should be familiar with replacing an element in the ArrayList using the set() method in Java. To be more confident in your learning try practising it over and over. Feel free to replug whenever you feel like it. Good luck and happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION