Aside from Reimeus answer:
What would I need to do if I would want to make only 1 piece of the array true and decide where this true should go in the same way as I decide the length of the array.
Using fact that boolean
arrays are by default filled with false
values you just need to read once index of element that should be set to true
just like you did with length
s
boolean[][] cells = new boolean[x][y];
int trueX = scanner.nextInt();
int trueY = scanner.nextInt();
cells[trueX][trueY] = true;
also don't forget to remove Arrays.fill(cells[i], true);
Secondly is there a way in which I could replace the true statement with "*" and the false statement with "-"
You can create second loop that will iterate over elements of row and if it its value is true
print *
if not -
like in this code:
for (int i = 0; i < cells.length; i++) {
for (int j = 0; j < cells[i].length; j++) {
if (cells[i][j])
System.out.print("* ");
else
System.out.print("- ");
}
System.out.println();
}
or just replace "true"
string from result of Arrays.toString() with *
and "false"
with "-"
for (int i = 0; i < cells.length; i++) {
System.out.println(Arrays.toString(cells[i]).replace("true", "*")
.replace("false", "-"));
}