Two-dimensional boolean array?
Hi,
I'a beginner in Java language. I saw the following exercise from the following web:
http://introcs.cs.princeton.edu/14array/
I tryied to solve it, but can't get it. Please help .
Write a code fragment that prints the contents of a two-dimensional boolean array, using * to represent true and a space to represent false. Include row and column numbers.
below is my effort....
Code :
public void 2Ddisplay() {
for (int x = 0; x < 2Darray[0].length; x++) {
for (int y = 0; y < 2Darray.length; y++) {
if (pixels[x][y] == true) {
System.out.println("*");
} else {
System.out.println(" ");
}
}
}
}
boolean 2Darray[][];
int w,h;
Thank you
Re: Two-dimensional boolean array?
The instructions say to use * to represent true and space for false. Therefore you need a 2D char array not a boolean array.
Re: Two-dimensional boolean array?
There's only a 2 possible values, therefore a 2d boolean array will work perfectly fine (not to mention that the problem specifically says to represent the contents of a 2d boolean array as such :P).
You say you can't get it, what did you mean by that statement? Are your outputs wrong? Or are you getting an exception/compile error? If so, please post the message.
One thing I can see right away is that you're printing out each element on it's own line. You might want to consider using System.out.print to print out a row, then use an empty System.out.println to advance to the next row.