如何将 [[Ljava.lang.String;@7defb4fb] 转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32345533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:22:39  来源:igfitidea点击:

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

javaarrays

提问by iQuestProgrammer

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

我正在尝试打印String,下面是代码:

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.

** 我的问题是在 Java 中打印和数组的数组。首先要认识到它的嵌套数组,因此当我尝试遍历数组并打印其元素时,它打印的是地址的地址而不是元素的地址。

[Ljava.lang.String;@7defb4fb]

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

采纳答案by Naman Gala

You can try instanceofand then castit to String[].

您可以尝试instanceof然后其转换为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);
    }
}

回答by mvd

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.

您需要迭代数组并一一打印字符串。如果单个对象是数组,那么您也需要迭代它们。

回答by SacJn

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

这将取决于您要添加到 array 的数据类型。即使您在数组上迭代仍然使用SOP打印对象,也会调用toString()对象类型类上的方法。

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

因此,如果您要添加自己的类的对象,请提供自定义toString()方法。现在迭代数组。

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

对象的 toString()方法的默认实现将产生以下格式的字符串 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.

Arrays.deepToString(objArray)如果 objArray 包含您定义的类的对象,则问题的最佳解决方案是与自定义 toString() 实现一起使用。

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]

该程序的输出应为
[sfo.ArrayPrint@19f953d, sfo.ArrayPrint@1fee6fc]

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

但是要获取实际内容,如果我将 toString() 方法覆盖到我的类中,如下所示

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

You get the output as below

你得到如下输出

[12,15]

[12,15]

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

所以你看, Arrays.deepToString() 只有当你为你自己的类对象有自定义的 toString() 实现时才有用。

回答by karim mohsen

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 reflectionor use instanceof

您需要迭代数组并打印每个元素。如果数组元素是数组,您也需要迭代抛出它们,您可以通过反射或使用instanceof

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

例如:-if(obj instanceof int[])if(obj instanceof String[])