1. 程式人生 > >TreeSet應用的例子

TreeSet應用的例子

compareto () score err java com ble super setname

public class Student implements Comparable<Student>{
    int stuno;
String name;
int score;
public int getStuno() {
    return stuno;
}
public void setStuno(int stuno) {
    this.stuno = stuno;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getScore() { return score; } public void setScore(int score) { this.score = score; } public Student(int stuno, String name, int score) { super(); this.stuno = stuno; this.name = name; this.score = score; } @Override public String toString() { return "Student [stuno=
" + stuno + ", name=" + name + ", score=" + score + "]"; } @Override public int compareTo(Student o) { if(this.score-o.score==0){ return this.stuno-o.stuno; } return this.score-o.score; } }
import java.util.TreeSet;

public class TestTree {
    public static void main(String[] args) {
    TreeSet
<Student> set=new TreeSet<Student>(); set.add(new Student(1, "james", 90)); set.add(new Student(2, "go", 80)); set.add(new Student(3, "jobs",65)); set.add(new Student(4, "bill",65)); set.add(new Student(5, "aoache",80)); for(Student s:set){ System.out.println(s); } } }

TreeSet應用的例子