— Scott McNealy, Sun Microsystems Chairman, President and ceo


BE the compiler, advanced



Download 40,57 Mb.
Pdf ko'rish
bet533/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   529   530   531   532   533   534   535   536   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

BE the compiler, advanced

ArrayList dogs1 = new ArrayList();


ArrayList animals1 = new ArrayList();


 

List list = new ArrayList();

ArrayList dogs = new ArrayList();

ArrayList animals = dogs;

List dogList = dogs;

ArrayList objects = new ArrayList();

List objList = objects;

ArrayList objs = new ArrayList();
 
Compiles?
be the compiler 
exercise


collections 
with 
generics
you are here
4
577
import 
java.util.*
;
public class SortMountains {
LinkedList

mtn = new LinkedList

();
class NameCompare 
implements Comparator
{
public int compare(Mountain one, Mountain two) {
return 
one.name.compareTo(two.name)
;
}
}
class HeightCompare 
implements Comparator
{
public int compare(Mountain one, Mountain two) {
return (
two.height - one.height
);
}
}
public static void main(String [] args) {
new SortMountain().go();
}
public void go() {
mtn.add(new Mountain(“Longs”, 14255));
mtn.add(new Mountain(“Elbert”, 14433));
mtn.add(new Mountain(“Maroon”, 14156));
mtn.add(new Mountain(“Castle”, 14265));
System.out.println(“as entered:\n” + mtn);
NameCompare nc = new NameCompare();
Collections.sort(mtn, nc)
;
System.out.println(“by name:\n” + mtn); 
HeightCompare hc = new HeightCompare();
Collections.sort(mtn, hc)
;
System.out.println(“by height:\n” + mtn); 
}
}
class Mountain {
String name
;
int height
;
Mountain(String n, int h)
{
name = n
;
height = h
;
}
public String toString( )
{
return name + “ “ + height
;
}
File Edit Window Help ThisOne’sForBob
%java SortMountains
as entered:
[Longs 14255, Elbert 14433, Maroon 14156, Castle 14265]
by name:
[Castle 14265, Elbert 14433, Longs 14255, Maroon 14156]
by height:
[Elbert 14433, Castle 14265, Longs 14255, Maroon 14156]
Output:
Did you notice that the height list is 
in DESCENDING sequence ? : )
Solution to the “Reverse 
Engineer” sharpen exercise


578
 
chapter 16
Given the following compilable statement: 
 
Collections.sort(myArrayList);
1. What must the class of the objects stored in 
myArrayList
implement?
Comparable
2. What method must the class of the objects stored in 
myArrayList
implement?
compareTo( )
3. Can the class of the objects stored in 
myArrayList
implement both
Comparator
AND 
Comparable
?
yes
Given the following compilable statement: 
 
Collections.sort(myArrayList, myCompare);
4. Can the class of the objects stored in 
myArrayList
implement 
Comparable
?
yes
5. Can the class of the objects stored in 
myArrayList
implement 
Comparator
?

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   529   530   531   532   533   534   535   536   ...   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