2009年6月13日 星期六

DVD資料排序

將dvdinfo.txt內資料
Donnie Darko/sci-fi/Gyllenhall, Jake
Raiders of the Lost Ark/action/Ford, Harrison
2001/sci-fi/??
Caddy Shack/comedy/Murray, Bill
Star Wars/sci-fi/Ford, Harrison
Lost in Translation/comedy/Murray, Bill
Patriot Games/action/Ford, Harrison
依照title排序...

1 則留言:

瑞克 提到...

/**
* @(#)DVDAdd.java
*
*
* @author shitRick
* @version 1.00 2009/6/13
*/
import java.io.*;
import java.util.*;

class DVDInfo implements Comparable-DVDInfo- {
String title;
String genre;
String leadActor;
DVDInfo(String t, String g, String a) {
title = t; genre = g; leadActor = a;
}
public String toString() {
return title+" "+genre+" "+leadActor+"\n";
}
public String getTitle() {
return title;
}
public int compareTo(DVDInfo d) {
return title.compareTo(d.getTitle());
}
}
public class DVDAdd {

ArrayList-DVDInfo- dvdList = new ArrayList-DVDInfo-();
String[] tokens;
public DVDAdd() {
}
void populateList() {
dvdList.add(new DVDInfo(tokens[0], tokens[1], tokens[2]));
}

public static void main(String[] args) {
DVDAdd da = new DVDAdd();
try {
File file = new File("dvdinfo.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String data = br.readLine();
while(data != null){
//System.out.println(data);
da.tokens = data.split("/");
da.populateList();
//new DVDInfo(tokens[0], tokens[1], tokens[2]);
//System.out.println();
data = br.readLine();
}
fr.close();
}catch(IOException e) {}
System.out.println(da.dvdList);
Collections.sort(da.dvdList);
System.out.println(da.dvdList);

}
}