Just playing around with displaying values in a two dimensional array and noticed my code prints some hashcodes
and then the values. But I am not printing the array object itself (as done in the post here) or at least explicitly calling the toString
or hashcode
method of the array object. Instead, I'm directly accessing and printing the values in the array using arrayObj[i]
and arrayObj[i][j]
.
Here's my code:
class PrintTwoDimArray {
public int [][] createArray () {
int counter = 0;
int[][] intArray = new int [2][4];
for (int i = 0; i < intArray.length; i++) {
for (int j = 0; j < intArray[i].length; j++) {
intArray[i][j] = ++counter;
}
}
return intArray;
}
public void printArray ( int [][] arrayObj ) {
for (int i = 0; i < arrayObj.length; i++) {
System.out.print(arrayObj[i] + " ");
for (int j = 0; j < arrayObj[i].length; j++) {
System.out.print(arrayObj[i][j] + " ");
}
System.out.println();
}
}
}
class TestPrintTwoDimArray {
public static void main (String[] args) {
PrintTwoDimArray twoDim = new PrintTwoDimArray();
int [][] multiArray = new int [2][4];
multiArray = twoDim.createArray();
twoDim.printArray(multiArray);
}
}
My output is as follows:
It seems that my code is somehow calling the toString
or hashcode
method of the array. Thoughts? How can I modify to print just the values?
javac and java version 1.8.0