-1

I wrote following program but i want to remove "," from last element, what should i do?Is there any in-build method?

public class arraySort 
{
public static void main(String[] args)
{
    int a[]={2,7,5,6,9,8,4,3,1};
    for(int i=0;i<a.length;i++)
    {
        for(int j=i+1;j<a.length;j++)
        {
            if(a[i]<a[j])
            {
                int temp;
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }               
        }
    }

    System.out.print("Descending order:{");
    for(int i=0;i<a.length;i++)
    {
            System.out.print(a[i]+",");
    }
    System.out.println("}");

}

}

output:Descending order:{9,8,7,6,5,4,3,2,1,}

1 Answers1

-1

or try this

public static void main(String[] args) {
            int a[]={2,7,5,6,9,8,4,3,1};
            System.out.print("Descending order:{");
            for(int i=0;i<a.length;i++)
            {
                for(int j=i+1;j<a.length-1;j++)
                {
                    if(a[i]<a[j])
                    {
                        int temp;
                        temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                    }
                }
                if (i == a.length-1) {
                    System.out.print(a[i]);
                } else {
                     System.out.print(a[i]+",");
                }
            }
        System.out.println("}");
    }
jarvo69
  • 7,908
  • 2
  • 18
  • 28
  • i get it but i want to know if there is such type of method is exist in java or not? Thanks for help – Vikram Gondane Aug 04 '16 at 15:21
  • answer is "NO". you will either need to store the result in a string and then using String's `lastIndexOf` method, remove last occurrence of ",". – jarvo69 Aug 04 '16 at 15:44