How can I convert int[]
to comma-separated String in Java?
int[] intArray = {234, 808, 342};
Result I want:
\"
This is the pattern I always use for separator-joining. It's a pain to write this boilerplate every time, but it's much more efficient (in terms of both memory and processing time) than the newfangled Stream
solutions that others have posted.
public static String toString(int[] arr) {
StringBuilder buf = new StringBuilder();
for (int i = 0, n = arr.length; i < n; i++) {
if (i > 0) {
buf.append(", ");
}
buf.append(arr[i]);
}
return buf.toString();
}