— Scott McNealy, Sun Microsystems Chairman, President and ceo



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

collections 
with 
generics
you are here
4
535
The Collections.sort() 
method sorts a list of 
Strings alphabetically.
Adding Collections.sort() to the Jukebox code
File Edit Window Help Chill
%java Jukebox1
[Pink Moon, Somersault, Shiva Moon, Circles, Deep 
Channel, Passenger, Listen]
[Circles, Deep Channel, Listen, Passenger, Pink 
Moon, Shiva Moon, Somersault]
import java.util.*;
import java.io.*;
public class Jukebox1 {
ArrayList songList = new ArrayList();
public static void main(String[] args) {
new Jukebox1().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(“/”);
songList.add(tokens[0]);
}
}
Call the static Collections 
sort() method, then print the 
list again. The second print out 
is in alphabetical order!
Before calling sort().
After calling sort().


536
 
chapter 16
But now you need Song objects
not just simple Strings.
Now your boss wants actual Song class instances in the list, not just 
Strings, so that each Song can have more data. The new jukebox 
device outputs more information, so this time the fi le will have four 
pieces (tokens) instead of just two.
The Song class is really simple, with only one interesting feature—
the overridden toString() method. Remember, the toString() 
method is defi ned in class Object, so every class in Java inherits the 
method. And since the toString() method is called on an object 
when it’s printed (System.out.println(anObject)), you should 
override it to print something more readable than the default 
unique identifi er code. When you print a list, the toString() 
method will be called on each object.

Download 40,57 Mb.

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