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.

1

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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());

Comments

0

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

Comments

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());
}

Comments

0

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

Comments

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]);
            }
        }

Comments

0

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

Comments

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.