Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collections and array Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KB book
pg-573

Collections and Array


Putting it all together, our DVDInfo class should now look like this:

Now, when we invoke Collections.sort(dvdList); we get
[2001 sci-fi ??
, Caddy Shack comedy Murray, Bill
, Donnie Darko sci-fi Gyllenhall, Jake
, Lost in Translation comedy Murray, Bill
, Patriot Games action Ford, Harrison
, Raiders of the Lost Ark action Ford, Harrison
, Star Wars sci-fi Ford, Harrison
]


how does the call to Collections.sort(dvdList); sort the elements of DVDInfo ?
we are passing the whole dvdlist to sort method.
now how would it call compareTo method and that also with DVDINFO object
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how does the call to Collections.sort(dvdList); sort the elements of DVDInfo ?

DVDInfo implements the Comparable interface. You can imagine that the Collections class will at some point cast the each object into a Comparable, and call the compareTo method. You can actually look at the source of the Collections class to see how it's done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic