0

Good evening. I am reviewing for an intro to Java exam and the instructor wrote a question I seem to be missing something.

Consider:

 int[][] mat = new int[3][4];
for (int row = 0; row < mat.length; row++)
{
for(int col = 0; col < mat[0].length; col++)
{
if (row < col)
mat[row][col] = 1;
else if (row == col)
mat[row][col] = 2;
else
mat[row][col] = 3;
}
}

What are the contents of mat after the code segment has been executed?

The selection of answers I won't bother to list specifically as I don't require a specific answer to this. I need to understand what is supposed to happen. I plugged into an IDE and I get a gibberish answer if I add a System.out.print(mat);

"[[I@139a55"

While the instructor is expecting a selection of integers in either a 3x4 or 4x3 array. No integers exceed 3.

Any illumination on this would be invaluable to helping me understand the relationship between the for loops and the array to be done.

GoldenDesign
  • 115
  • 5
  • 1
    If your main question is how to print the array elements, the previous question at http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array may help. – paisanco Jul 18 '16 at 00:12
  • If you want to see for yourself, do this: `for(int i=0; i – RaminS Jul 18 '16 at 00:12

1 Answers1

0

Java array type doesn't provide a specific implementation of toString() method to pretty print its content. What you are seeing it's a representation of the reference itself in the JVM but it's rather meaningless for your purpose.

I'd suggest you to try with

for (int i = 0; i < mat.length; ++i)
  System.out.println(Arrays.toString(mat[i]));

Which uses Arrays.toString method, which prints an array in an human readable way (basically by calling toString on each element)

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Also see [Arrays#deepToString](https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#deepToString-java.lang.Object:A-) which is even easier to use, but won't print the line breaks. – The111 Jul 18 '16 at 00:23