Converting Arrays to String

posted 29 Dec 2010, 08:49 by Sanjeev Kumar   [ updated 11 Nov 2011, 06:10 ]
Sometimes it may be required to display an array in string format. Expected result should be that all the elements of the Arrays gets printed out the way it is done for any collection. However the default toString() method is not very informative and does not include any array content. I have seen developers doing weird stuff to achieve this which includes writing helper methods and quite a few lines of code. 

However there are easier ways available with in Java API to achieve the same output. To provide more useful representations of arrays, various toString methods (and the deepToString method) were added to the Arrays class in JDK 1.5. Those methods can be used when available, as in :

Arrays.deepToString(myObjectArray); //recursive

If you need an alternative, you can simply convert the array to a collection, as in


Happy coding !!!