Check out the set(int index, E element) method in the List interface
Top answer 1 of 6
446
Check out the set(int index, E element) method in the List interface
2 of 6
160
You can replace the items at specific position using set method of ArrayList as below:
list.set( your_index, your_item );
But the element should be present at the index you are passing inside set() method else it will throw exception.
Also you can check oracle doc here
Java Code Geeks
javacodegeeks.com โบ home โบ core java
Java ArrayList Insert/Replace At Index - Java Code Geeks
January 2, 2022 - Use the ArrayList.add(int index, Object value) method to add any object or element at the specific index of ArrayList and use ArrayList.set(int index, E value) to replace the value at the specific index of ArrayList in java.
04:13
HOW TO REPLACE THE PARTICULAR ELEMENTS IN AN ARRAYLIST JAVA ...
01:26
How to replace the object in the ArrayList? | Java Collection ...
How to add or replace an element in the ArrayList using ...
10:46
Search and Replace & Merge Two ArrayLists | Intro to Java - YouTube
05:52
set or replace an element in an arraylist java program - YouTube
14:02
Learn Java Programming - ArrayList replaceAll Method Tutorial - ...
CodeGym
codegym.cc โบ java blog โบ java collections โบ how to replace an element in java arraylist
How to Replace an Element in Java ArrayList
October 11, 2023 - The method returns the same ArrayList element that is just replaced. 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); } }
Programiz
programiz.com โบ java-programming โบ library โบ arraylist โบ replaceall
Java ArrayList replaceAll()
Rather, it replaces all value of the arraylist with new values from operator. import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<String> languages = new ArrayList<>(); // add elements to the ArrayList languages.add("java"); ...
TutorialsPoint
tutorialspoint.com โบ How-to-replace-an-element-of-an-ArrayList-in-Java
How to replace an element in a list using Java
July 30, 2019 - Following example uses replaceAll() method to replace all the occurance of an element with a different element in a list. import java.util.*; public class Main { public static void main(String[] args) { List list = Arrays.asList("one Two three ...
W3Schools
w3schools.com โบ java โบ ref_arraylist_replaceall.asp
Java ArrayList replaceAll() Method
The replaceAll() method replaces every item in a list with the result of performing an operation on the item. The operation can be defined by a lambda expression that is compatible with Java's UnaryOperator interface.
JavaGoal
javagoal.com โบ home โบ how to replace element in an arraylist?
replace element in an ArrayList and set() method - JavaGoal
July 27, 2020 - How to replace element in an ArrayList and how we can use the set(int index,E element) method. This method replace the existing element
GeeksforGeeks
geeksforgeeks.org โบ java โบ replacing-all-occurrences-of-specified-element-of-java-arraylist
Replacing All Occurrences of Specified Element of Java ArrayList - GeeksforGeeks
July 23, 2025 - In this program, we will create an ArrayList of Integers and add elements in it using add() method, then we will replace all occurrences of 21 with 200 using replaceAll() method. ... // Java program for Replacing All Occurrences of Specified ...
GeeksforGeeks
geeksforgeeks.org โบ java โบ replace-an-element-from-arraylist-using-java-listiterator
Replace an Element From ArrayList using Java ListIterator - GeeksforGeeks
April 3, 2023 - ArrayList<String> name = new ArrayList<>(); name.add("Yash"); name.add("Akash"); name.add("Amar"); name.add("Abhishek"); name.add("Rajnikanth"); Let us assume that we need to replace "Rajnikanth" with "Mohit". Now, using a ListIterator, we will iterate to the last element of the list and then replace it - ListIterator<String> iterator = name.listIterator(); while(iterator.hasNext()) { iterator.next(); } iterator.set("Mohit"); ... // java program to replace an element // from ArrayList using Java ListIterator import java.util.ArrayList; import java.util.ListIterator; class GFG { public static v
W3Resource
w3resource.com โบ java-tutorial โบ arraylist โบ arraylist_replaceall.php
Java ArrayList replaceAll Method - w3resource
Java ArrayList.replaceAll() Method with example: The replaceAll() method is used to replace each element of this list with the result of applying the operator to that element. Errors or runtime exceptions thrown by the operator are relayed to ...
Top answer 1 of 4
318
Use the set method to replace the old value with a new one.
list.set( 2, "New" );
2 of 4
36
If you are unaware of the position to replace, use list iterator to find and replace element ListIterator.set(E e)
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.equals("Two")) {
//Replace element
iterator.set("New");
}
}
TutorialsPoint
tutorialspoint.com โบ replace-all-elements-of-arraylist-with-with-java-collections
Replace All Elements Of ArrayList with with Java Collections
In order to replace all elements of ArrayList with Java Collections, we use the Collections.fill() method. The static void fill(List list, Object element) method replaces all elements in the list with the specified element in the argument.
TutorialsPoint
tutorialspoint.com โบ replace-an-element-in-an-arraylist-using-the-listiterator-in-java
Replace an element in an ArrayList using the ListIterator in Java
An element in ArrayList can be replaced using the ListIterator method set(). This method has a single parameter i.e. the element that is to be replaced and the set() method replaces it with the last element returned by the next() or previous() methods. A program that demonstrates this is given ...
TutorialsPoint
tutorialspoint.com โบ replace-all-occurrences-of-specified-element-of-arraylist-with-java-collections
Replace all occurrences of specified element of ArrayList with Java Collections
June 25, 2020 - In order to replace all occurrences of specified element of ArrayList with Java Collections, we use the Collections.replaceAll() method. This method returns true if list contains one or more elements e such that (oldVal==null ? e==null : oldVal.eq