— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet490/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   486   487   488   489   490   491   492   493   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

sorting 
a list
Challenge #1
Sort the songs in alphabetical order
SongList.txt
Pink Moon/Nick Drake
Somersault/Zero 7
Shiva Moon/Prem Joshua
Circles/BT
Deep Channel/Afro Celts
Passenger/Headmix
Listen/Tahiti 80
This is the file the jukebox device 
writes. Your code must read the file, 
then manipulate the song data.


collections 
with 
generics
you are here
4
531
Here’s what you have so far, without the sort:
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); 
}
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]);
}
}
We’ll store the song titles in 
an ArrayList of Strings.
Nothing special here... just read the file and 
call the addSong() method for each line.
The addSong method works just like the Quiz-
Card in the I/O chapter--you break the line 
(that has both the title and artist) into two 
pieces (tokens) using the split() method.
We only want the song title, so 
add only the first token to the 
SongList (the ArrayList).
The method that starts loading the 
file and then prints the contents of 
the songList ArrayList.
File Edit Window Help Dance

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   486   487   488   489   490   491   492   493   ...   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