Check out the set(int index, E element) method in the List interface

Answer from TotoroTotoro on Stack Overflow
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java list โ€บ replace element at a specific index in a java arraylist
Replace Element at a Specific Index in a Java ArrayList | Baeldung
April 3, 2025 - First, we create an ArrayList with five elements. Then, we replace the third element with the value 7, having index 2 with 3. Finally, we can see that index 2 with value 7 is removed from the list and updated with the new value 3. Also, note that the list size is not impacted. In this quick article, we learned how to replace an element at a specific index in Java ArrayList.
Discussions

Which method is used to replace the element at a specific index in an ArrayList in Java? Question 6Select one: a. set() b. replace() c. modify() d. assign()
The set() method takes two parameters: the index of the element to be replaced and the new element that will replace it. It updates the ArrayList by replacing the element at the specified index with the new element. More on studocu.com
๐ŸŒ studocu.com
1
October 2, 2023
java - ArrayList replace element if exists at a given index? - Stack Overflow
How to replace element if exists in an ArrayList at a given index? More on stackoverflow.com
๐ŸŒ stackoverflow.com
(Java)Edit element in an arrayList
When in doubt check the docs https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html You can do ArrayList.get(int index) Animals animals = a.get(index); animals.doStuff(); More on reddit.com
๐ŸŒ r/learnprogramming
12
3
June 19, 2016
A better way to find the index of an element in an array?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
23
3
August 27, 2022
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Java ArrayList Insert/Replace At Index - Java Code Geeks
January 2, 2022 - List2 values before insertion - ... or replace the existing value with the new value of ArrayList, use set(int index, E element) with the index and new value....
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ java_arraylist_replace_at_specific_index
Java ArrayList replace at specific index
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
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 - 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.
๐ŸŒ
Vultr
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ util โ€บ ArrayList โ€บ set
Java ArrayList set() - Replace Element | Vultr Docs
November 13, 2024 - Use the set() method to replace the element at the specified index. ... ArrayList<String> colors = new ArrayList<>(Arrays.asList("Red", "Green", "Blue")); colors.set(1, "Yellow"); // Replace "Green" with "Yellow" System.out.println(colors); ...
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ replace an existing item in arraylist
Replace an Existing Item in ArrayList
January 12, 2023 - Once we have the index, we can use set() method to update the replace the old element with a new item. Find the index of an existing item using indexOf() method. Use set(index, object) to update with the new item.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ how-to-replace-a-element-in-java-arraylist
How to Replace a Element in Java ArrayList? - GeeksforGeeks
July 23, 2025 - To replace an element in Java ArrayList, set() method of java.util. An ArrayList class can be used. The set() method takes two parameters the indexes of the element that has to be replaced and the new element. The index of an ArrayList is zero-based.
Find elsewhere
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ collections framework โ€บ java arraylist โ€บ add element(s) at specified index to arraylist in java
Add Element(s) at Specified Index to ArrayList in Java
August 7, 2023 - The new elements are always added to the end of current arraylist, unless we specifically mention the index where we want to add the new elements. ArrayList<String> arraylist = new ArrayList<>(); arraylist.add("apple"); // [apple] arraylist.add("banana"); // [apple, banana] //Adding a new element at index position 1 arraylist.add(1, "grapes"); // [apple, grapes, banana] //Adding multiple elements element at index position 0 arraylist.add(0, Arrays.asList("date", "guava")); // [date, guava, apple, grapes, banana]
๐ŸŒ
JavaGoal
javagoal.com โ€บ home โ€บ how to replace element in an arraylist?
replace element in an ArrayList and set() method - JavaGoal
July 27, 2020 - We have seen how to add and remove elements from ArrayList in java. But sometimes, there may be a situation when we want to replace the element in ArrayList. We can use set(int index, E element) to replace the element. The set(index, E element) method is used to set the specified element at ...
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ java-arraylist-replace-at-specific-index
How to Replace an Element in Java ArrayList at Specific Index: Step-by-Step Guide with Example Method โ€” javaspring.net
Return Value: Returns the old element that was replaced (of type E, the generic type of the ArrayList). Behavior: Replaces the element at index with element and returns the original element.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-replace-an-element-in-a-list
Java Program to Replace an Element in a List - GeeksforGeeks
January 7, 2026 - An ArrayList is created and initialized with integer values. The set(1, 200) method replaces the element at index 1 with 200. The updated list is printed to show the replaced value.
๐ŸŒ
CodePal
codepal.ai โ€บ code generator โ€บ java arraylist replace index
Java ArrayList Replace Index - CodePal
December 21, 2023 - The ArrayListUtils class provides a static method called replaceIndex that takes an ArrayList of objects, the old index of the values to be replaced, and the new index where the values should be moved to.
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ replace-element-at-a-specific-index-in-a-java-arraylist
Replace Element at a Specific Index in a Java ArrayList โ€” javathinking.com
We first create an ArrayList named fruits and add some elements to it. Then we use the set method to replace the element at index 1 (which was "Banana") with "Mango".
๐ŸŒ
Java67
java67.com โ€บ 2016 โ€บ 08 โ€บ how-to-replace-element-of-arraylist-in-java.html
How to replace an element of ArrayList in Java? Example | Java67
Our example replaces the 2nd element of the ArrayList by calling the ArrayList.set(1, "Introduction to Algorithms") because the index of the array starts from zero.
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java โ€บ util โ€บ arraylist
Replace ArrayList elements using index example - Java Code Geeks
June 6, 2013 - Use set(int index, Object obj) method of ArrayList, using a specified element and a specified index. The method replaces an element at the specified index of the arrayList with the given element.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ java-programs โ€บ replace-element-within-the-arraylist.aspx
Java program to replace element within the ArrayList
December 31, 2023 - Given an ArrayList and we have to replace element of it using Java program. ... import java.util.ArrayList; public class ExArrayReplace { public static void main(String[] args) { // Create an object of arrayList. ArrayList arrayList = new ArrayList(); //Add elements to arraylist. arrayList.add("55"); arrayList.add("25"); arrayList.add("368"); System.out.println("Original ArrayList..."); for (int index = 0; index < arrayList.size(); index++) System.out.println(arrayList.get(index)); // Enter the position and number for replace.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ replace-element-in-arraylist-in-java
Replace Element in Arraylist Java - Javatpoint
Replace Element in Arraylist Java with java tutorial, features, history, variables, programs, operators, oops concept, array, string, map, math, methods, examples etc.