Discuss / Java / 用score进行排序

用score进行排序

Topic source

由于基本类型int并不能使用compareTo函数,因此在定义score时将int改为Integer:

class Person implements Comparable<Person>{    String name;    Integer score;    Person(String name, int score){        this.name = name;        this.score = score;    }    public int compareTo(Person other){        return this.score.compareTo(other.score);    }    public String toString(){        return this.name +","+this.score;    }}

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]

你这是学糊涂了,两个int比较:

this.score==other.score


  • 1

Reply