wood burning stoves*
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
Author

Collections and array Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Collections and array Problem
 
Similar Threads
Putting an object instance into an ArrayList
Putting an object instance into an ArrayList
Reading data from a file and storing into array of strings problem
Sorting Collections
Collections.sort() on Array List