-2

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. Suppose we have a conversion method from ArrayList to String [] as described in answers of the first link I've referred to:

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, i.e, the for loop towards the end.

If I would print out just the String[] it would give me a garbage value like [Ljava.lang.String;@5636bc0a. This I guess is probably because of problems with the jvm returns toArray as an object.

Why is it this way and what is the work around for this?

I need a String [] that gives me a meaningful value. 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:

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. 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. So the problem is not with System.out.println().

Suppose I do a .toString() conversion I would need to convert it back to String[] by doing something like stockArr.split(""). I wanted to know if there is another work around for that.

EDIT 2

This has nothing to do with printing Arrays, it has to do with conversion of List to an Array of Strings.

Community
  • 1
  • 1
agenthost
  • 748
  • 2
  • 9
  • 24
  • So what you want is a way to print out meaningful information if the value is a `String[]`? Like, if you were doing `System.out.println(stockArr)`, you want the contents of the array to show up? – Makoto Oct 05 '15 at 19:47
  • Yes exactly, instead of values like `[Ljava.lang.String;@5636bc0a` and so on. – agenthost Oct 05 '15 at 19:48
  • Does this *only* apply when you're using `System.out.println`? – Makoto Oct 05 '15 at 19:48
  • The proper way to String[] from list is list.toArray(new String[list.size()]) Therefore you should do String sockArry = list.toArray(new String[list.size()]) – Ya Wang Oct 05 '15 at 19:49
  • @Makoto no it does not apply only to `System.out.println` and @YaWang that would still return object values. – agenthost Oct 05 '15 at 20:06
  • Okay, that's good to know. I'll go ahead and retract my dupe vote. But, this raises the question: what the heck is wrong with `toArray(String[] arr)`? – Makoto Oct 05 '15 at 20:07
  • Thanks, that is the main problem `toArray(String[] arr)` returns object values and not string values. – agenthost Oct 05 '15 at 20:10
  • I don't buy that. Try using `String[] stockArr = stockList.toArray(new String[stockList.size()]);` instead. You're *guaranteed* to get back the type of array you specify as the parameter to `toArray`, so you wouldn't get back an `Object[]` unless you were newing up an `Object[]`. – Makoto Oct 05 '15 at 20:21
  • Did you try compiling what you just wrote? – agenthost Oct 05 '15 at 20:38
  • I understand even less what you're asking about now. That `toArray` method is the appropriate way to get an array from a `List`. – Sotirios Delimanolis Oct 05 '15 at 20:40
  • Are you trying to parse text and marshall it into a `String[]`? – Sotirios Delimanolis Oct 05 '15 at 20:54
  • Yes that's exactly it. All I want is my output of String[] should look like for example [1,2,3...] because I need it to set the value of a JAX-WS Holder. – agenthost Oct 05 '15 at 20:58
  • This is very frustrating. You say _that's exactly it_ (concerning the deserialization), but then you go and describe that you need the exact opposite process, ie. the serialization. Which one is it? And why doesn't `Arrays.toString()` solve your problem? That takes a `String[]` and returns a `String` that looks like `[1,2,3]`. – Sotirios Delimanolis Oct 05 '15 at 21:12

2 Answers2

0

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. To print the string representation of an array, use 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.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Thanks for the reply, but this is not what I am looking for, hope the edit makes it more clear now. – agenthost Oct 05 '15 at 20:04
  • @agenthost An array is not a string. These are two different types. You don't have to convert the array to a string to pass it to the function. Just make sure that whoever is going to print the values does this by using `Arrays.toString(array)`. – M A Oct 05 '15 at 20:09
  • It has nothing to do with printing the values! – agenthost Oct 05 '15 at 20:12
  • @agenthost I think you should re-read my answer. [An array is an object in Java](http://stackoverflow.com/questions/8781022/is-an-array-an-object-in-java). That's why `toArray(String[] arr)` effectively returns an _object_. The value you saw `[Ljava.lang.String;@5636bc0a` is just the representation of this array _as an object_ but it doesn't mean it does not contain string values. – M A Oct 05 '15 at 20:20
0

There is a useful library function Arrays.toString(arr)

String[] arr = { "a", "b" };
System.out.println(Arrays.toString(arr));
//Output: [a, b]
James Wierzba
  • 16,176
  • 14
  • 79
  • 120