0

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);
    }
iQuestProgrammer
  • 309
  • 2
  • 5
  • 18
  • Yes it gave me this output [[Folder, TrackerDemo1, ckuser, released ]]. But again I need the individual elements in the array.This isnt much useful. I can apply string methods to extract each element but that will add additional Lines of Code to my source code – iQuestProgrammer Sep 02 '15 at 06:04
  • What type of object are you storing inside object array? – Naman Gala Sep 02 '15 at 06:04
  • Cast it to `String` then. `String value = (String) obj;` – M. Shaw Sep 02 '15 at 06:05
  • Casted and printed - _System.out.println((String) obj);_ recieve this output again _[Ljava.lang.String;@7defb4fb_ – iQuestProgrammer Sep 02 '15 at 06:09
  • possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – RAS Sep 02 '15 at 07:22
  • You could have simply used Arrays.deepToString, no need to go type of object using instance of. – SacJn Sep 02 '15 at 11:18

5 Answers5

7

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);
    }
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
1

You need to iterate the array and print the strings one by one. If the individual objects are arrays, then you need to iterate them as well.

mvd
  • 2,596
  • 2
  • 33
  • 47
0

It will depend on what kind of data you are adding to array. Even if you iterate on array still printing object using SOP will call toString() method on the type class of the object.

So if you are adding objects of your own class then provide customized toString() method. And now iterate on array.

default implementation of object's toString() method will produce a string in following format getClass().getName() + '@' + Integer.toHexString(hashCode())

Best solution for your problem is use Arrays.deepToString(objArray) with custom toString() implementation if objArray contains objects of your defined class.

Look at the example below

public class ArrayPrint {
int age;

public static void main(String[] args) {
    ArrayPrint obj1 = new ArrayPrint ();
    obj1.age = 12;

    ArrayPrint obj2 = new ArrayPrint ();
    obj2.age = 15;

    ArrayPrint[] ageArr = {obj1, obj2};
    System.out.println(Arrays.deepToString(ageArr));

}

}

Output for this program shall be
[sfo.ArrayPrint@19f953d, sfo.ArrayPrint@1fee6fc]

But to get the actual content, if I override toString() method to my class like below

public String toString ()
{
    return ""+age;
}

You get the output as below

[12,15]

So you see, Arrays.deepToString() shall be useful only if you have custom toString() implementation for your own class objects.

SacJn
  • 777
  • 1
  • 6
  • 16
0

You need to iterate the array and print each element.if the array element is array you need to iterate throw them as well you can do that by reflection or use instanceof

ex :- if(obj instanceof int[]) or if(obj instanceof String[])

karim mohsen
  • 2,164
  • 1
  • 15
  • 19
0

I had a similar problem. This is happening because Arrays.toString()is used on an array of objects which store separate elements and it fails when the array contains another array of objects within it, just like in your case. deepToString can be used to resolve this. I used Arrays.deepToString(Object[]) to convert the array of objects to string and then worked on the resultant string to find specific fields. Check out the second paragraph of the answer provided by @polygenelubricants in What is this: [Ljava.lang.Object;?

Check https://www.baeldung.com/java-tostring-array for more details on the solution.