Just in case, if someone tries to do this without java 8, there is a pretty good trick. List.toString () already returns a collection that looks like this:
[1,2,3]
Depending on your specific requirements, this can be handled until the list items contain [] or.
For example:
list.toString().replace("[","").replace("]","")
or if your data may contain square brackets:
String s=list.toString(); s = s.substring(1,s.length()-1)
will give you a pretty reasonable result.
One array element in each row can be created as follows:
list.toString().replace("[","").replace("]","").replaceAll(",","\r\n")
I used this method to create html hints from a list in a small application with something like:
list.toString().replace("[","<html>").replace("]","</html>").replaceAll(",","<br>")
If you have an array then start with Arrays.asList (list) .toString () instead
I completely agree that this is not optimal, but it is not as inefficient as you think, and it is quite simple to read and understand. This, however, is rather inflexible - in particular, do not try to separate replaceAll elements if your data can contain commas and use the substring version if you have square brackets in your data, but for an array of numbers this is pretty much perfect.