zoukankan      html  css  js  c++  java
  • Arrays.sort()方法给出了,只要该类实现了comparable方法,那就可以实现排序

    package 测试Arrays排序功能的强大性能;
    
    import java.util.Arrays;
    
    public class Ceshi {
        public static void main(String[] args) {
            Employee[] employees = new Employee[3];
            employees[0] = new Employee("lgq", 2309.98);
            employees[1] = new Employee("zcp", 234232.8);
            employees[2] = new Employee("wyl", 643.98);
            System.out.println(Arrays.deepToString(employees));
            Arrays.sort(employees);
            System.out.println(Arrays.deepToString(employees));
        }
    }
    
    class Employee implements Comparable<Employee> {
    
        String name;
        double salary;
    
        public Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getSalary() {
            return salary;
        }
    
        public void setSalary(int salary) {
            this.salary = salary;
        }
    
        public void raiseSalary(double byPercent) {
            double raise = salary * byPercent / 100;
            salary += raise;
        }
    
        @Override
        public String toString() {
            return "{" + this.name + ":" + this.salary + "}" ;
        }
    
        @Override
        public int compareTo(Employee o) {
    
            if (this.salary < o.salary) return -1;
            if (this.salary > o.salary) return 1;
    
            return 0;
        }
    }
  • 相关阅读:
    BZOJ 1021 循环的债务
    BZOJ 1019 汉诺塔
    BZOJ 1018 堵塞的交通
    BZOJ 1017 魔兽地图
    BZOJ 1016 最小生成树计数
    Luogu 3008 [USACO11JAN]道路和飞机Roads and Planes
    Luogu 3625 [APIO2009]采油区域
    Luogu 4139 上帝与集合的正确用法
    Luogu 3629 [APIO2010]巡逻
    Luogu 3626 [APIO2009]会议中心
  • 原文地址:https://www.cnblogs.com/sharysea/p/12051403.html
Copyright © 2011-2022 走看看