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

3
  • this string is redundant: System.out.print(arrayObj[i] + " "); Commented Nov 15, 2014 at 8:20
  • @nikis I used the empty string to create a space between the values. Commented Nov 15, 2014 at 8:26
  • @sedeh: This is not the hash code: it's the address in the memory where your object is stored. Commented Nov 15, 2014 at 8:37

3 Answers 3

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]));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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 

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.