Remove last separator from print statement

前端 未结 6 729
情深已故
情深已故 2021-01-26 09:34

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) {
            


        
6条回答
  •  情深已故
    2021-01-26 09:52

    You may use a StringBuilder as follows:

        StringBuilder sb =new StringBuilder();
        int[] sort = {1,2,3,4,5,7,89,4,9,6,11,23,178,29};
        for(int i : sort) sb.append(i+", ");
        if(sb.length()>2)       sb.delete(sb.length()-2, sb.length());
        System.out.println(sb.toString());
    

    EDIT: This is not the sorting algorythm. This is just an example of how you can present it without the last comma.

提交回复
热议问题