-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.

13
  • 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? Commented Oct 5, 2015 at 19:47
  • Yes exactly, instead of values like [Ljava.lang.String;@5636bc0a and so on. Commented Oct 5, 2015 at 19:48
  • Does this only apply when you're using System.out.println? Commented Oct 5, 2015 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()]) Commented Oct 5, 2015 at 19:49
  • @Makoto no it does not apply only to System.out.println and @YaWang that would still return object values. Commented Oct 5, 2015 at 20:06

2 Answers 2

0

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

String[] arr = { "a", "b" };
System.out.println(Arrays.toString(arr));
//Output: [a, b]
1
  • The question has changed a bit since I posted this answer Commented Oct 6, 2015 at 17:30
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.

4
  • Thanks for the reply, but this is not what I am looking for, hope the edit makes it more clear now. Commented Oct 5, 2015 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). Commented Oct 5, 2015 at 20:09
  • It has nothing to do with printing the values! Commented Oct 5, 2015 at 20:12
  • @agenthost I think you should re-read my answer. An array is 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. Commented Oct 5, 2015 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.