Skip to content
Advertisement

How do I convert [[Ljava.lang.String;@7defb4fb] to a String

I am trying to print the String, below is the code:

Object[] objArr = data.get(key);
for (Object obj : objArr)
{
    System.out.println(obj);
    cell.setValue(obj);
}

but I get following output in the console:

[[Ljava.lang.String;@7defb4fb]

I have tried the following snippets:

System.out.println(Arrays.toString(objArr));
System.out.println(Arrays.asList((objArr)));
System.out.println(objArr[k]);

But all of these are giving similar strange output:

[[Ljava.lang.String;@7defb4fb]
[[Ljava.lang.String;@7defb4fb]
[Ljava.lang.String;@7defb4fb

How do I get the string value out of it?

**

Edit:

** My problem was to print and array of an array in Java. First thing was to recognize that its nested arrays and hence when I try to iterate over the array and print its elements it was printing the address of the address instead of the element.

[Ljava.lang.String;@7defb4fb]

My problem was to recognize this as an array and iterate over this array again in order to print the elements. Hence here was the solution

if (obj instanceof String[]) {
        String[] strArray = (String[]) obj;
        System.out.println(Arrays.toString(strArray));
        // System.out.println(obj);
    }

Advertisement

Answer

You can try instanceof and then cast it to String[].

Sample code:

String[] strArr = {"anc", "asda"};
Object[] objArr = {strArr, strArr}; // Array of String Arrays
for (Object obj : objArr) {
    if (obj instanceof String[]) {
        String[] strArray = (String[]) obj;
        System.out.println(Arrays.toString(strArray));
        // System.out.println(obj);
    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement