简体   繁体   English

为什么在将 ArrayList 转换为 String[] 时 list.toArray() 是一个对象?

[英]Why is list.toArray() an object while converting ArrayList to String[]?

To make it very clear this is not a duplicate of these questions Convert ArrayList to String and Convert ArrayList containing strings but it is of relevance to them.明确地说,这不是这些问题Convert ArrayList to StringConvert ArrayList contains strings的重复,但它与它们相关。 Suppose we have a conversion method from ArrayList to String [] as described in answers of the first link I've referred to:假设我们有一个从ArrayListString []的转换方法,如我提到的第一个链接的答案中所述:

List<String> stockList = new ArrayList<String>();
stockList.add("stock1");
stockList.add("stock2");

String[] stockArr = new String[stockList.size()];
stockArr = stockList.toArray(stockArr);

for(String s : stockArr)
   System.out.println(s); 

With the print statement my output would look like this:使用打印语句,我的输出将如下所示:

stock1
stock2

But what if I wanted my output to be in an array format (like [stock1,stock2] ) and I excluded the conversion to String , ie, the for loop towards the end.但是,如果我希望我的输出采用数组格式(例如[stock1,stock2] )并且我排除了到String的转换,即最后的 for 循环呢?

If I would print out just the String[] it would give me a garbage value like [Ljava.lang.String;@5636bc0a .如果我只打印出String[]它会给我一个垃圾值,如[Ljava.lang.String;@5636bc0a This I guess is probably because of problems with the jvm returns toArray as an object.我猜这可能是因为 jvm 将toArray作为对象返回的问题。

Why is it this way and what is the work around for this?为什么会这样,有什么解决方法?

I need a String [] that gives me a meaningful value.我需要一个String []来给我一个有意义的值。 I need it in this format because I am using this conversion to call a JAX-WS function in my project which accepts only String[] values:我需要这种格式,因为我使用这种转换在我的项目中调用一个 JAX-WS 函数,它只接受String[]值:

myJaxWSObj.setValue(String[] myArrayOfStrings);

EDIT编辑

Thanks for the answers, but some of you must have misunderstood the question.感谢您的回答,但你们中的一些人一定误解了这个问题。 I want to convert ArrayList to String[] and not to String.我想将 ArrayList 转换为 String[] 而不是 String。 So doing any sort of .toString() wouldn't help me much because as I mentioned above I need to call a JAX-WS class which accepts only String[] values.所以做任何类型的.toString()对我没有多大帮助,因为正如我上面提到的,我需要调用一个只接受String[]值的 JAX-WS 类。 So the problem is not with System.out.println() .所以问题不在于System.out.println()

Suppose I do a .toString() conversion I would need to convert it back to String[] by doing something like stockArr.split("") .假设我进行了.toString()转换,我需要通过执行类似stockArr.split("")将其转换回String[] I wanted to know if there is another work around for that.我想知道是否还有其他解决方法。

EDIT 2编辑 2

This has nothing to do with printing Arrays, it has to do with conversion of List to an Array of Strings.这与打印数组无关,它与将列表转换为字符串数组有关。

Arrays are objects.数组是对象。 Printing them using System.out.println() will call the default Object#toString() method which returns the object class name with a hashcode value.使用System.out.println()打印它们将调用默认的Object#toString()方法,该方法返回带有哈希码值的对象类名称。 To print the string representation of an array, use Arrays.toString(Object[]) :要打印数组的字符串表示,请使用Arrays.toString(Object[])

Returns a string representation of the contents of the specified array.返回指定数组内容的字符串表示形式。 If the array contains other arrays as elements, they are converted to strings by the Object.toString() method inherited from Object, which describes their identities rather than their contents.如果数组包含其他数组作为元素,它们会通过继承自 Object 的Object.toString()方法转换为字符串,该方法描述的是它们的身份而不是它们的内容。

There is a useful library function Arrays.toString(arr)有一个有用的库函数Arrays.toString(arr)

String[] arr = { "a", "b" };
System.out.println(Arrays.toString(arr));
//Output: [a, b]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM