Array values are mutable. Here's a quick solution: ```java import java.util.ArrayList; import java.util.List; import java.util.Random; public class ToDORNotTwoD { public static void main(String[] args) { int[][] array = new int[10][10]; List coordinateList = new ArrayList<>(); randomizeMatrix(array); printMatrix(array); manipulateMatrix(array, coordinateList); printCoordinateList(coordinateList); printMatrix(array); } private static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { System.out.printf("%3d", matrix[i][j]); } System.out.println(); } System.out.println(); } private static void printCoordinateList(List coordinateList) { System.out.println(coordinateList.toString()); System.out.println(); } private static void randomizeMatrix(int[][] matrix) { Random rand = new Random(); for (int i = 0; i < matrix.length; ++i) { for (int j = 0; j < matrix[0].length; ++j) { matrix[i][j] = rand.nextInt(100); } } } private static void manipulateMatrix(int[][] matrix, List coordinateList) { for (int i = 0; i < matrix.length; ++i) { for (int j = 0; j < matrix[0].length; ++j) { if (matrix[i][j] % 3 == 0) { matrix[i][j] = -1; String pair = "( " + String.valueOf(i) + " , " + String.valueOf(j) + ")"; coordinateList.add(pair); } } } } } ``` Answer from srikarvedantam on teamtreehouse.com
Top answer
1 of 3
1
Array values are mutable. Here's a quick solution: ```java import java.util.ArrayList; import java.util.List; import java.util.Random; public class ToDORNotTwoD { public static void main(String[] args) { int[][] array = new int[10][10]; List coordinateList = new ArrayList<>(); randomizeMatrix(array); printMatrix(array); manipulateMatrix(array, coordinateList); printCoordinateList(coordinateList); printMatrix(array); } private static void printMatrix(int[][] matrix) { for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[0].length; j++) { System.out.printf("%3d", matrix[i][j]); } System.out.println(); } System.out.println(); } private static void printCoordinateList(List coordinateList) { System.out.println(coordinateList.toString()); System.out.println(); } private static void randomizeMatrix(int[][] matrix) { Random rand = new Random(); for (int i = 0; i < matrix.length; ++i) { for (int j = 0; j < matrix[0].length; ++j) { matrix[i][j] = rand.nextInt(100); } } } private static void manipulateMatrix(int[][] matrix, List coordinateList) { for (int i = 0; i < matrix.length; ++i) { for (int j = 0; j < matrix[0].length; ++j) { if (matrix[i][j] % 3 == 0) { matrix[i][j] = -1; String pair = "( " + String.valueOf(i) + " , " + String.valueOf(j) + ")"; coordinateList.add(pair); } } } } } ```
2 of 3
0
Another way to phrase an answer, is to point out the fact that the SIZE of an array is immutable, while the values in the array are "mutable" (you can replace the values as you'd like, but putting a string in an array does not suddenly make it mutable.)
๐ŸŒ
Learnerslesson
learnerslesson.com โ€บ JAVA โ€บ Java-Replace-Array-Elements.htm
Java - Replace Array Elements
Before replacing the second element ... After replacing the second element Mohan Neal Paul Kriti Salim ยท So, in the above code we have created a Array and initialised to the variable x. String[] arr = {"Mohan", "John", "Paul", "Kriti", "Salim"}; Now, let us see, how the values are positioned ...
Discussions

java - Comparing and replacing values in an array - Software Engineering Stack Exchange
I am currently working on a program that replaces the value in an array if the value next to it is the same as the current value. So If the array is [0,0,0,1,0,1,0], when the program runs it'll tur... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
How do I replace something in an array?
Not sure if I should give the answer directly: To access the variable in array you can use e.g. System.out.println(array[1] ) will print โ€œ1.0โ€ from your example. To replace the value you can just assign the value to the index you want. array[index] = value to assign More on reddit.com
๐ŸŒ r/javahelp
3
1
August 24, 2023
Java: Replacing the values in an array and shifting the elements in an array - Stack Overflow
So I've been tasked with completing this program for my AP Computer Science class: Write the code that takes an array of doubles and moves all the values greater than the average to fill the ar... More on stackoverflow.com
๐ŸŒ stackoverflow.com
December 17, 2016
How do I replace an array element in Java? - Stack Overflow
Kindly make the question clear. Are u asking something like how to replace a particular value with other or how to remove null values in a string array? More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Stack Exchange
softwareengineering.stackexchange.com โ€บ questions โ€บ 421661 โ€บ comparing-and-replacing-values-in-an-array
java - Comparing and replacing values in an array - Software Engineering Stack Exchange
Instead, rewrite your code with for (int x : ar). Since you need to evaluate two adjacent elements, you'll also need a local variable int previous which would remember the previous value from the array.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ how do i replace something in an array?
r/javahelp on Reddit: How do I replace something in an array?
August 24, 2023 -

I have an array with placeholder terms, call it double[] array = {0,1,2,3,4,5,6,7}. How do I write a statement that replaces one of the terms with a variable of the same type?

for example, replacing index 1 with a variable that is equal to 5.6.

Top answer
1 of 2
4
Not sure if I should give the answer directly: To access the variable in array you can use e.g. System.out.println(array[1] ) will print โ€œ1.0โ€ from your example. To replace the value you can just assign the value to the index you want. array[index] = value to assign
2 of 2
1
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://i.imgur.com/EJ7tqek.png ) 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.
๐ŸŒ
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); } }
๐ŸŒ
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
*/ public class ArrayListSetDemo { public static void main(String[] args) { // let's create a list first List<String> top5Books = new ArrayList<String>(); top5Books.add("Clean Code"); top5Books.add("Clean Coder"); top5Books.add("Effective Java"); top5Books.add("Head First Java"); top5Books.add("Head First Design patterns"); // now, suppose you want to replace "Clean Coder" with // "Introduction to Algorithms" System.out.println("ArrayList before replace: " + top5Books); top5Books.set(1, "Introductoin to Algorithms"); System.out.println("ArrayList after replace: " + top5Books); } } Output Array
Find elsewhere
๐ŸŒ
YouTube
youtube.com โ€บ pace edu.
How to Change Elements of Array in Java - YouTube
replace element in list javaHow to Change Elements of ArrayList in Javareplace element in arraylist javareplace all elements in array javareplace element in ...
Published ย  March 26, 2022
Views ย  2K
๐ŸŒ
w3resource
w3resource.com โ€บ java-exercises โ€บ array โ€บ java-array-exercise-63.php
Java - Replace each element of the array with its product
May 9, 2025 - // Import the necessary Java class. import java.util.Arrays; // Define a class named 'solution'. class solution { // Method to find the product of every other element in an array. public static int[] find_Product_in_array(int[] nums) { int n = nums.length; // Initialize arrays to store left and right products. int[] left_element = new int[n]; int[] right_element = new int[n]; // Calculate left products. left_element[0] = 1; for (int i = 1; i < n; i++) { left_element[i] = nums[i - 1] * left_element[i - 1]; } // Calculate right products. right_element[n - 1] = 1; for (int j = n - 2; j >= 0; j--)
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 41195091 โ€บ java-replacing-the-values-in-an-array-and-shifting-the-elements-in-an-array
Java: Replacing the values in an array and shifting the elements in an array - Stack Overflow
December 17, 2016 - Any values less than the average that haven't been written over should be set to 0. First calculate the average and store it in a variable named average. Example: The array [1.0 , 1.0, 3.0 4.0 ], the average is 2.25 would be transformed to [3.0, ...
๐ŸŒ
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 - Step-2 ? Find the product of all array elements. Step-3 ? Divide the product value with the value of the respective index and replace the result.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ replace-every-element-of-the-array-by-its-next-element
Replace every element of the array by its next element - GeeksforGeeks
March 23, 2023 - Given an array arr, the task is to replace each element of the array with the element that appears after it and replace the last element with -1. ... Approach: Traverse the array from 0 to n-2 and update arr[i] = arr[i+1]. In the end, set a[n-1] ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 69067445 โ€บ remove-an-element-from-array-and-replace-with-0
java - Remove an element from array and replace with 0? - Stack Overflow
Here is an approach, similar to what I described in the comments above. import java.util.Arrays; public class main { // tip: arguments are passed via the field below this editor public static void main(String[] args) { int[] arr = {3, 5, 7, 8, 5, 12, 2}; remove(arr, 5); System.out.println(Arrays.toString(arr)); // [3, 7, 8, 5, 12, 2, 0] } public static void remove(int[] arr, int toRemove) { int idx = -1; // determine first occurrence of toRemove for(int i = 0; i < arr.length; i++) { if(arr[i] == toRemove) { idx = i; break; } } // if not found, return if(idx == -1) return; // shift other elements down for(int i = idx; i < arr.length-1; i++) { arr[i] = arr[i+1]; } // set last element to 0 arr[arr.length-1] = 0; } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ java-program-to-replace-each-element-of-array-with-its-next-element
JAVA Program to Replace Each Element of Array with its Next Element
import java.util.Arrays; public class Main { public static void main(String[] args) { // given array int arr[] = {4, 2, 3, 1, 5}; // Print the array elements System.out.println("Array elements are: " + Arrays.toString(arr)); // new array int newArr[] = new int[arr.length]; // assign the first element of the array to a temp variable int temp = arr[0]; // replacing current element with next element System.arraycopy(arr, 1, newArr, 0, arr.length - 1); // replace the last element of the array with first element newArr[newArr.length - 1] = temp; // updated array System.out.print("Updated array: "); for (int j : newArr) { System.out.print(j + " "); } } }