File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Collections and array Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Collections and array Problem" Watch "Collections and array Problem" New topic

Collections and array Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

KB book

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

OCPJP 6.0 93%
OCPJWCD 5.0 98%
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

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.

[My Blog]
All roads lead to JavaRanch
I agree. Here's the link:
subject: Collections and array Problem
It's not a secret anymore!