1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public class Test {
class YAD_Score implements java.io.Serializable{
private int score;
private String name;
private int temps;
public YAD_Score(int score, String name, int temps) {
this.score = score;
this.name = name;
this.temps = temps;
}
public int getScore() {
return score;
}
public String toString() {
return name + " " + score;
}
}
Vector scores = new Vector();
Vector scores2;
public Test() {
scores.add(new YAD_Score(100, "toto", 10));
scores.add(new YAD_Score(200, "tot", 10));
scores.add(new YAD_Score(50, "to", 10));
scores.add(new YAD_Score(300, "t", 10));
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("Highscore.sav")));
out.writeObject(scores);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("Highscore.sav")));
this.scores2 = (Vector) in.readObject();
in.close();
Collections.sort(scores2, new Comparator(){
public int compare(Object arg0, Object arg1) {
return ((YAD_Score) arg0).getScore() - (((YAD_Score) arg1).getScore());
}
});
for (int i = 0; i < scores2.size(); i++) {
System.out.println(scores2.get(i));
}
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
} catch (ClassNotFoundException cnfex) {
}
}
public static void main(String args[]) {
new Test();
}
} |