Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Nancy Melucci
Courses Plus Student 36,159 PointsReplacing values in an array
So...I have an assignment in which I am supposed to produce an array of 100 random integers in a 10x10 rectangle (done) and produce a list of i j coordinates for all the values in the array that are divisible by 3 (done) but finally I have to reprint the original array with -1s where the %3 = 0 values were. That is where I am stuck. This is an assignment about arrays not arraylists (so I am confused by the directive to replace values in an immutable integer array). I did produce the coordinate list using a String ArrayList. I can produce an arraylist that replaces the values with -1 but I lose my formatting. Can someone help me make my arraylist of integers with -1 placeholders format neatly as a 10x10? This seems like the only reasonable solution to me, and I am running out of time. Thanks NJM
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
public class ToDORNotTwoD {
public static void main(String[] args) {
int[][] array = new int[10][10];
ArrayList<String> coordinateList = new ArrayList<>();
ArrayList<Integer> placeHolder = new ArrayList<>();
int i = 0;
int j = 0;
for (i = 0; i < 10; i++) {
System.out.println(" ");
for (j = 0; j < 10; j++) {
array[i][j] = randomFill();
System.out.print(array[i][j] + "\t");
}
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (array[i][j] % 3 == 0) {
String pair = "( " + String.valueOf(i) + " , " + String.valueOf(j) + ")";
coordinateList.add(pair);
}
}
}
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
placeHolder.add(array[i][j]);
if (array[i][j] % 3 == 0) {
placeHolder.add(-1);
}
}
}
System.out.printf(placeHolder.toString(), " ");
System.out.println();
System.out.println(coordinateList.toString());
}
public static int randomFill() {
Random rand = new Random();
int randomNum = rand.nextInt((int) (Math.random() * 100) + 1);
return randomNum;
}
}
3 Answers

srikarvedantam
8,369 PointsArray values are mutable. Here's a quick solution:
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<String> 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<String> 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<String> 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);
}
}
}
}
}

Nancy Melucci
Courses Plus Student 36,159 PointsThat's much more help than I expected. Thank you.

srikarvedantam
8,369 PointsWelcome.

Kristian Gausel
14,661 PointsAnother 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.)