Build the string in the loop, but don't return until you have processed all the elements:
@Override String toString() {
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < a.length - 1; ++i) {
sb.append(a[i]);
sb.append(", ");
}
if (a.length > 0) {
sb.append(a[a.length - 1]);
}
sb.append("]");
return sb.toString();
}
I'm assuming that you want to append all elements in the array, you are not intentionally excluding the last one.
Using a StringBuilder is preferable to plain string concatenation, since it is more efficient to accumulate lots of strings that way.