This discussion is archived
2 Replies Latest reply: Jul 3, 2013 11:44 AM by rp0428 RSS

using comparable or comparator as parameter in method

user8744860 Newbie
Currently Being Moderated

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

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points