Here\'s a method for sorting an integer array. How can I remove the last separator form the output?
public void Sort(int[] sort) {
for (int a:sort) {
Couple of options:
public void Sort(int[] sort) {
for (int i = 0; i < sort.length; i++) {
System.out.print(sort[i]);
if (i < sort.length - 1) {
// not the last element. Add separator
System.out.print(" ,");
}
}
}
Another way:
public void Sort(int[] sort) {
String output = Arrays.toString(sort);
System.out.println(Arrays.toString(output).substring(1, output.length-1));
}