— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet524/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   520   521   522   523   524   525   526   527   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

TreeSets and 
sorting


collections 
with 
generics
you are here
4
565
What you MUST know about TreeSet...
import java.util.*;
public class TestTree {
public static void main (String[] args) {
new TestTree().go();
}
public void go() {
Book b1 = new Book(“How Cats Work”);
Book b2 = new Book(“Remix your Body”);
Book b3 = new Book(“Finding Emo”);

TreeSet tree = new TreeSet();
tree.add(b1);
tree.add(b2);
tree.add(b3);
System.out.println(tree);
}
}
class Book {
String title;
public Book(String t) {
title = t;
}
}
Sharpen your pencil
Look at this code. 
Read it carefully, then 
answer the questions 
below. (Note: there 
are no syntax errors 
in this code.)
1). What is the result when you compile this code?
2). If it compiles, what is the result when you run the TestTree class?
3). If there is a problem (either compile-time or runtime) with this code, how would you fix it?
TreeSet looks easy, but make sure you really understand what you need to 
do to use it. We thought it was so important that we made it an exercise so 
you’d have to think about it. Do NOT turn the page until you’ve done this. 
We mean it.


566
 
chapter 16
TreeSet elements MUST be comparable
TreeSet can’t read the programmer’s mind to figure out how the 
object’s should be sorted. You have to tell the TreeSet how.
class Book 

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   520   521   522   523   524   525   526   527   ...   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