1

I newbie in Java and learning it. I got a question related to array, can you please look into it?

I am getting following output: [[Ljava.lang.String;@7a982589 from below code:

String[][] multi = new String [][] {
{ "Scenarios", "Description", "1.0", "1.1", "1.2"},
{ "S1", "Verify hotel search", "Y", "Y", "Y"},
};
System.out.println(multi);

While if I place following:

System.out.println(multi[0][1]);

I am getting correct output. Description.

Now, why I am not getting entire array through "multi" variable.

Asvhini
  • 55
  • 6
  • A relevant question of what is going on: http://stackoverflow.com/questions/13498179/output-of-system-out-printlnobject – aug Sep 14 '13 at 03:46

7 Answers7

1

Use this util method, cause you have a 2D array. Read the api Arrays#deepToString(Object[])

Arrays.deepToString(multi);

Returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings.

nachokk
  • 14,363
  • 4
  • 24
  • 53
0

What you're seeing is the default toString() of arrays. If you want it pretty-printed, use Arrays.deepToString(arr);

Kayaman
  • 72,141
  • 5
  • 83
  • 121
0

That is because,instead of printing the value stored in the array, you are printing the state of the array. That is when you say System.out.println(multi); it implicitly means System.out.println(multi.toString());

Thirumalai Parthasarathi
  • 4,541
  • 1
  • 25
  • 43
0

Use java.util.Arrays.deepToString(multi). What you're seeing is the default Object.toString() since arrays don't override toString().

Jeremy
  • 326
  • 1
  • 5
0

System.out.println calls toString on object arguments to get their textual representation. Java arrays are also objects and they inherit toString from java.lang.Object:

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

multi will hold the reference to the array itself, and multi[0][1] will refer to the element in the array

upog
  • 4,965
  • 8
  • 42
  • 81
0

access each item from array and print it out:

for(int i=0;i<2;i++){
            for(int j=0;j<5;j++){
                System.out.println(multi[i][j]);
            }
        }
Minh Nguyen
  • 1,989
  • 3
  • 17
  • 30