-3

This is an unusual problem I have: I've created a 2d int array with dimensions of 3X3, no parameters and when I try to print it out on the screen it displays something like this : [[I@49d67b41. Any suggestions?

public static void main (String[]  args) {
    System.out.println(test2());
}

static int [][] test2 () {
    int [][] l = new int[3][3];
    return l;
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
tim
  • 467
  • 5
  • 12

2 Answers2

3

Use

System.out.println(Arrays.deepToString(test2));
BackSlash
  • 21,927
  • 22
  • 96
  • 136
Andres
  • 10,561
  • 4
  • 45
  • 63
  • 3
    Nope. You should use `Arrays.deepToString(test2)` in that specific case, as it's a 2d array. Otherwise it will print the same horrible string for each sub-array :) – BackSlash Apr 14 '14 at 21:15
  • +1 for being on the right lines. Better than my answer. – christopher Apr 14 '14 at 21:15
0

int[][] is a subclass of type Object. The default toString() method is being called, and you're seeing the hex code of your object. You need to implement your own method for printing out the int[][] type.

Example

for(int[] values : test2()) {
    for(int value : values) {
         System.out.print(int + " ");
    }
    System.out.println();
}
christopher
  • 26,815
  • 5
  • 55
  • 89