Hello friends its Vibhas can anybody explain me my doubts in this following code and also please cheak my comments wheather my understanding is correct or not...
My doubt part is marked as //??? part which is the last line please clarify it here is the code
import java.util.*;
public class Dvdinfo implements Comparable<Dvdinfo>
{
String title;
String genre;
String leadActor;
Dvdinfo(String t,String g,String k)
{
this.title = t;
this.genre = g;
this.leadActor = k;
}
public static void main(String[] args)
{
Dvdinfo dvi1= new Dvdinfo("sholay","male","Amitabh");
Dvdinfo dvi2= new Dvdinfo("Charas","male","jimmy");
Dvdinfo dvi3= new Dvdinfo("Lagaan","male","Aamir");
System.out.println(dvi1);
System.out.println(dvi2);
System.out.println(dvi3);
ArrayList<Dvdinfo> ard = new ArrayList<Dvdinfo>();
ard.add(dvi1);
ard.add(dvi2);
ard.add(dvi3);
Collections.sort(ard);// here the collection of list( i.e what "ard" contains) is not
//of type String but of type Dvdinfo so sorting will not be performed
// automatically we have to override the compareTo() method of
//Comparable interface i.e by implementing Compareable interface
//So it will search for the type " public int compareTo(Dvdinfo d)"
// in the following code so it can perform sorting hence compareTo() is
// called.....
System.out.println("Collections :"+ard);
}
public String toString()
{
return title + " " + genre + " " + leadActor +"\n";
}
public int compareTo(Dvdinfo d)
{
System.out.println(title +" --- "+d.getTitle());
return title.compareTo(d.getTitle());
// title takes the 1st title value "sholay" and d.getTitle() returns 2nd
// title i.e "Charas" similarly comparision of "sholay" and "Laagan"
// and comparision of "Charas" and "Lagaan" and after that compareTo()
// method sort this titles
}
public String getTitle()
{
return title;
}
// ??? Can anybody explain me how compareTo method is basically working i.e
// how
java API is implementing this compareTo() method