— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet507/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   503   504   505   506   507   508   509   510   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

the sort() 
method


collections 
with 
generics
you are here
4
549
Finally we know what’s wrong...
The Song class needs to implement Comparable
We can pass the ArrayList to the sort() method only if the 
Song class implements Comparable, since that’s the way the sort()
method was declared. A quick check of the API docs shows the 
Comparable interface is really simple, with only one method to 
implement:
public interface Comparable {
int compareTo(T o);
}
java.lang.Comparable
And the method documentation for compareTo() says
The big question is: what 
makes 
one song less than, 
equal to, or greater than 
another song?
You can’t implement the 
Comparable interface until you 
make that decision.
Returns:
a negative integer, zero, or a 
positive integer as this object 
is less than, equal to, or greater 
than the specifi ed object.
It looks like the compareTo() method will be called on one 
Song object, passing that Song a reference to a different 
Song. The Song running the compareTo() method has to 
fi gure out if the Song it was passed should be sorted higher, 
lower, or the same in the list.
Your big job now is to decide what makes one song greater 
than another, and then implement the compareTo() method 
to refl ect that. A negative number (any negative number) 
means the Song you were passed is greater than the Song 
running the method. Returning a positive number says 
that the Song running the method is greater than the Song 
passed to the compareTo() method. Returning zero means 
the Songs are equal (at least for the purpose of sorting... it 
doesn’t necessarily mean they’re the same object). You might, 
for example, have two Songs with the same title. 
(Which brings up a whole different can of worms we’ll look 
at later...)
Sharpen your pencil
Write in your idea and pseudo code (or 
better, REAL code) for implementing the 
compareTo() method in a way that will 
sort() the Song objects by title.
Hint: if you’re on the right track, it should 
take less than 3 lines of code!



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   503   504   505   506   507   508   509   510   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish