3

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:

enter image description here

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

Community
  • 1
  • 1
sedeh
  • 7,083
  • 6
  • 48
  • 65

3 Answers3

1

A two dimensional array is an array of arrays. The array you create (int[2][4]) looks like this

[ 0 ] -> [ 1, 2, 3, 4 ]
[ 1 ] -> [ 5, 6, 7, 8 ]

So when you only access the first dimension you will get the array that holds the second dimension.

int[][] arr = createArray();
System.out.println(arr[0]);

will output something like

[I@1db9742

To print an array's values you can use Arrays.toString(arr). In this case you can omit the inner loop, because Arrays.toString() will do it for you.

 public void printArray ( int [][] arrayObj ) {
    for (int i = 0; i < arrayObj.length; i++) {
        System.out.println(Arrays.toString(arrayObj[i]));
    }
}
René Link
  • 48,224
  • 13
  • 108
  • 140
0

The hash values come from your first System.print sentence.

System.out.print(arrayObj[i] + " ");

With this sentence you are printing an Object (an array) and therefore Java is invoking the default toString method.

Esteban Collado
  • 1,840
  • 1
  • 14
  • 15
0

When you print

arrayObj[i]

you get the default Object toString() from an array. You could use Arrays.toString(int[]) to make that a String like

System.out.println(Arrays.toString(arrayObj[i]));

Alternatively, you can use Arrays.deepToString(Object[]) to print your multi-dimensional array without a loop

System.out.println(Arrays.deepToString(arrayObj));

Edit

You could use formatted output to build your table.

public static void printArray(int[][] arrayObj) {
    for (int i = 0; i < arrayObj.length; i++) {
        System.out.printf("%03d: ", i + 1);
        for (int val : arrayObj[i]) {
            System.out.printf("% 4d ", val);
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    int[][] table = { { 3, 1, 2 }, { 5, 1, 4 }, { 100, 200, 300 } };
    printArray(table);
}

Output is

001:    3    1    2 
002:    5    1    4 
003:  100  200  300 
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • `deepToString` works sleek and I've noted it. In this scenario though, I like to print the values as a grid. Not sure how to implement `Arrays.toString(int[])` here bcos the second print statement has double int values. Something like this won't work: `System.out.println(Arrays.toString(arrayObj[i][j]))`. Further thoughts? – sedeh Nov 15 '14 at 08:42
  • @sedeh Write your own `toString(int[])` that builds a `String` with a `StringBuilder` and format it how you like. Also, your "double int values" is a single `int` value at it's array position of `i,j`. – Elliott Frisch Nov 15 '14 at 08:44