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

How to replace array element with respect to Index on another array without using inbuilt methods?
replace array element with respect to Index i can't even tell what you're asking More on reddit.com
🌐 r/learnjavascript
37
16
October 15, 2022
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()
In this example, the element at index 1 ("Banana") is replaced with "Mango" using the set() method. The resulting ArrayList is [Apple, Mango, Orange]. ... AI answers may contain errors. Please double check important information and use responsibly. ... What is the purpose of the Collection Hierarchy in Java... 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 replacing elements of an array - Stack Overflow
Is there a problem with replacing elements of this array like this at the end of the f-for modifier? also, with array[100] i just chose a big number so it wouldn't max out. ... In this case i advise you to use single variables. If you need to keep track of the remaining soldiers on each iteration, then use 2 arrays ("soldiers" for "a" and "catapults" for "b") and use the index ... More on stackoverflow.com
🌐 stackoverflow.com
May 28, 2018
🌐
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 - Nevertheless in ArrayList replace is pretty convenient to implement using the set method. arrayList.set(int index, dataType arrayListElement); The method takes 2 parameters. int index — The first one is the index of the element in ArrayList. ...
🌐
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.
🌐
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 › replace an existing item in arraylist
Replace an Existing Item in ArrayList
January 12, 2023 - ArrayList<String> list = new ArrayList<>(List.of("A", "B", "C", "D")); int index = list.indexOf("C"); list.set(index, "C_NEW"); Assertions.assertEquals("C_NEW", list.get(index)); We can make the whole replacing process in a single statement ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-replace-a-element-in-java-arraylist
How to Replace a Element in Java ArrayList? - GeeksforGeeks
July 23, 2025 - So, to replace the first element, 0 should be the index passed as a parameter. ... // Java program to demonstrate set() Method of ArrayList // Where Index is Within Bound import java.io.*; import java.util.*; class GFG { public static void ...
🌐
Reddit
reddit.com › r/learnjavascript › how to replace array element with respect to index on another array without using inbuilt methods?
r/learnjavascript on Reddit: How to replace array element with respect to Index on another array without using inbuilt methods?
October 15, 2022 -

Here's the code if anybody stumbles upon this post and wants to help

let arrValue = [1, 0, 0, 1, 1, 0, 1, 0, 0]; 
let arrIndex = [2, 3, 4, 7]; 

I can change the values with push method and cannot do it with respect to indices in arrIndex.

function indexSwitcher(arr){ 
    newArr = []; 
    for (let i = 0; i < arr.length; i++){ 
        if (arr[i] === 1){ 
            newArr[newArr.length] = 0; 
        } 
        else{ 
            newArr[newArr.length] = 1; 
        } 
    } 
    console.log(newArr); 
}

indexSwitcher(arrValue );
//output: [0, 1, 1, 0, 0, 1, 0, 1, 1] The above solution works but I don't know how to make it work with respect to indices in second array which is arrIndex here.

Please don't give the solution just the guidance so that I can work it on my own.

The final output should be [1, 0, 1, 0, 0, 0, 1, 1, 0]
🌐
Learnerslesson
learnerslesson.com › JAVA › Java-Replace-Array-Elements.htm
Java - Replace Array Elements
JAVA SPRING SPRINGBOOT HIBERNATE HADOOP HIVE ALGORITHMS PYTHON GO KOTLIN C# RUBY C++ HTML CSS JAVA SCRIPT JQUERY ... Let us say, we have a Array that contains three names, Mohan, John, Paul, Kriti and Salim. And we want to replace the name John with a new name Neal. public class MyApplication { public static void main(String[] args) { String[] arr = {"Mohan", "John", "Paul", "Kriti", "Salim"}; System.out.println("\nBefore replacing the second element\n"); for (String str : arr) { System.out.println(str); } arr[1] = "Neal"; System.out.println("\nAfter replacing the second element\n"); for (String str : arr) { System.out.println(str); } } }
🌐
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
Example: For list = [A, B, C], ... an element in a Java ArrayList at a specific index is simple with the set(int index, E element) method....
🌐
Sololearn
sololearn.com › en › Discuss › 1826212 › how-to-replace-existing-index-in-array-in-java
How to Replace existing index in array in Java | Sololearn: Learn to code for FREE!
The length of your array is 5. The issue is that array[5] doesn't exist : the last element is at array[4], because index in arrays starts at 0. So write : int x = array.length ; array[x - 1] = 5 ; 2nd Jun 2019, 10:45 AM · Théophile · + 2 · Dear friends Thanks both of you. code works · 2nd Jun 2019, 11:56 AM · Bakar Jokhio · Answer · Learn more efficiently, for free: Introduction to Python · 7.1M learners · Introduction to Java ·
🌐
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 - 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]
🌐
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
I think you can use a for-loop to find that certain item that matches certain criteria and then using the index from the for-loop you insert the item to the list at this index.Delete ... Yes, you can iterate through an ArrayList using Iterator, for-each or just for loop but becareful if you are removing elements as it can cause ConcurrentModficiationException.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-replace-an-element-at-a-specific-index-of-the-vector-in-java
How to Replace an Element at a Specific Index of the Vector in Java? - GeeksforGeeks
July 23, 2025 - The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in the java.util package and implements the List interface, so we can use all the methods of List interface here. Examples · Input : Vector= ["Harry","Steve","Vince","David","Matt"],Index=1,Element ...
🌐
Vultr
docs.vultr.com › java › standard-library › java › util › ArrayList › set
Java ArrayList set() - Replace Element | Vultr Docs
November 13, 2024 - This code initializes an ArrayList of colors, replaces the second element "Green" with "Yellow", and prints the updated list. Be aware that using an invalid index will throw an IndexOutOfBoundsException. Always check if the index is within the bounds of the list size before attempting to set a value.
🌐
TutorialsPoint
tutorialspoint.com › java-program-to-replace-element-of-integer-array-with-product-of-other-elements
JAVA Program to Replace Element of Integer Array with Product of Other Elements
December 30, 2022 - In this article, we will see how to replace array elements by the product of other array elements by using Java programming language. int arr[] = { 2, 3, 1, 4, 6 } At Index-0 value will be = 3 * 1 * 4 * 6 = 72 At Index-1 value will be = 2 * 1 * 4 * 6 = 48 At Index-2 value will be = 2 * 3 * 4 * 6 = 144 At Index-3 value will be = 2 * 3 * 1 * 6 = 36 At Index-4 value will be = 2 * 3 * 1 * 4 = 24 So, the updated array elements are {72, 48, 144, 36, 24}
🌐
Stack Overflow
stackoverflow.com › questions › 50565199 › java-replacing-elements-of-an-array
java replacing elements of an array - Stack Overflow
May 28, 2018 - Is there a problem with replacing elements of this array like this at the end of the f-for modifier? also, with array[100] i just chose a big number so it wouldn't max out. ... In this case i advise you to use single variables. If you need to keep track of the remaining soldiers on each iteration, then use 2 arrays ("soldiers" for "a" and "catapults" for "b") and use the index of the cycle., that is k or a counter that counts how many days have been passed (how many line have been red).