题解 | #集合排序#

集合排序

http://www.nowcoder.com/practice/4d12899bb255459bbf2b3635b3d32817

import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner;

public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Customer customer1 = new Customer("小明",scanner.nextInt()); Customer customer2 = new Customer("小军",scanner.nextInt()); Customer customer3 = new Customer("小红",scanner.nextInt()); List customers = new ArrayList<>(); customers.add(customer1); customers.add(customer2); customers.add(customer3);

    //write your code here......
    Collections.sort(customers);

    System.out.println(customers);

}

}

class Customer implements Comparable{ private String name; private int consumption;

public Customer(String name, int consumption) {
    this.name = name;
    this.consumption = consumption;
}

@Override
public String toString() {
    return "Customer{" +
            "name='" + name + '\'' +
            ", consumption=" + consumption +
            '}';
}

//write your code here......
@Override
public int compareTo(Customer o){
    return o.consumption - this.consumption;
}

}

全部评论

相关推荐

09-22 09:15
已编辑
安徽理工大学 Java
点赞 评论 收藏
分享
三轮业务+一轮HR&nbsp;俺有点没招了。。。
此刻我身在乌云中:hr打电话问我能不能提前实习,我说不可以,然后他说那可能要横向排序等结果。结果五分钟立马挂了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务