— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet496/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   492   493   494   495   496   497   498   499   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)


();
public static void main(String[] args) {
new Jukebox3().go();
}
public void 
go()

getSongs(); 
System.out.println(songList);
Collections.sort(songList);
System.out.println(songList); 
}
void 
getSongs()
{
try {
File file = new File(“SongList.txt”);
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line= reader.readLine()) != null) {
addSong(line);
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
void 
addSong(String lineToParse)
{
String[] tokens = lineToParse.split(“/”);
 
 
Song nextSong = new Song(tokens[0], tokens[1], tokens[2], tokens[3]);
 
 
songList.add(nextSong);

}
Change to an ArrayList of Song 
objects instead of String.
Create a new Song object using the four tokens 
(which means the four pieces of info in the song file 
for this line), then add the Song to the list.
Changing the Jukebox code to use Songs 
instead of Strings
Your code changes only a little—the file I/O code is the same, 
and the parsing is the same (String.split()), except this time 
there will be four tokens for each song/line, and all four will be 
used to create a new Song object. And of course the ArrayList 
will be of type instead of .


538
 
chapter 16
It won’t compile !
Something’s wrong... the Collections class clearly shows there’s a 
sort() method, that takes a List. 
ArrayList is-a List, because ArrayList implements the List interface, 
so... it should work. 
But it doesn’t!
The compiler says it can’t find a sort method that takes an 
ArrayList, so maybe it doesn’t like an ArrayList of Song 
objects? It didn’t mind an ArrayList, so what’s the 
important difference between Song and String? What’s the 
difference that’s making the compiler fail?
File Edit Window Help Bummer

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   492   493   494   495   496   497   498   499   ...   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