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
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.
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
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
(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
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
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
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]
Top answer 1 of 5
189
arrayList.set(index i,String replaceElement);
2 of 5
5
If you're going to be requiring different set functionaltiy, I'd advise extending ArrayList with your own class. This way, you won't have to define your behavior in more than one place.
// You can come up with a more appropriate name
public class SizeGenerousArrayList<E> extends java.util.ArrayList<E> {
@Override
public E set(int index, E element) {
this.ensureCapacity(index+1); // make sure we have room to set at index
return super.set(index,element); // now go as normal
}
// all other methods aren't defined, so they use ArrayList's version by default
}
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 ...
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.