How to convert String[] to String in Java without using for loop ?
I know how to convert using for loop but I want to convert it without using any loop (for or while).
How to convert String[] to String in Java without using for loop ?
I know how to convert using for loop but I want to convert it without using any loop (for or while).
Use Arrays.toString() to convert it.
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.
Arrays.toString()
Example
String[] arr = new String[2];
arr[0] = "Str 1"; arr[1] = "Str 2";
System.out.println(Arrays.toString(arr));
You can use Arrays.toString() form Doc
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.
The value returned by this method is equal to the value that would be returned byArrays.asList(a).toString(), unless a isnull, in which case"null"is returned.
But you you see the code it also uses the loop:
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(String.valueOf(a[i]));
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}