I’m trying to print out an 2D-array by traversing through it with a nested for-loop within a for-loop.
char[][] largeArray = {{'&','&','^','#','@','@','@','@','&','*','*','*'},
{'#','&','&','^','@','@','@','@','*','*','*','*'}}
for (int r = 0; r < 2; r++)
{
for (int c = 0; c < 12; c++)
{
System.out.print(largeArray[r][c]);
}
}
What it prints out is a single line of everything within that array
is there a way to print the first line of that array( everything in the first bracket)
then the next line or next bracket? I don’t want any extra space between the first line and the second line. so I can’t really just use System.out.println();
Any ideas?
You need to print a newline after printing each rows:
You can also consider using
Arrays.toStringmethod to print your array, to avoid using nested loops like this:Also, in order to further make your code look good, you can use an enhanced for-loop, instead of the old loop, to avoid indexing the array like this: