首页 > 学院 > 开发设计 > 正文

Comparable接口

2019-11-08 18:53:10
字体:
来源:转载
供稿:网友

如果一个数组对象要进行比较,则这个对象的实例类要实现Compareable接口

“` public class Employee implements Comparable{ /** * name name */ PRivate String name; private double salary;

public Employee(String name, double salary) { this.name = name; this.salary = salary;}public int compareTo(Employee o) { return Double.compare(salary, o.salary);}public static void main(String[] args) {Employee[] s = new Employee[3]; s[0] = new Employee("hssdst",8); s[1] = new Employee("dfsfds",6); s[2] = new Employee("dfsfds",7); Arrays.sort(s); for(Employee emo : s) { System.out.println(emo.getName()+","+emo.getSalary()); //结果为dfsfds,6.0、dfsfds,7.0、hssdst,8.0 }}

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表