2 Replies Latest reply: Jul 3, 2013 1:44 PM by rp0428 RSS

    using comparable or comparator as parameter in method

    user8744860

      Hi,

       

      I was going through some lectures online and found that to compare or even swap, the use of comparable or comparator argument like

       

      public static boolean less(Comparable v,Comparable w)

      {

           return v.compareTo(w)<0;

      }

       

      public static void swap(Comparable []a,int i,int j)

      {

           Comparable swap=a[i];

           a[i]=a[j];

           a[j]=swap;

      }

       

      I did not get the use of passing Comparable or Comparator to the function as parameters. Object as parameter could have been used too?

       

      Please help me understand this.

       

      Regards

       

      Abhinav