集合案例(Java)

本文介绍了两个Java集合的实战案例。第一个是使用TreeMap实现键盘录入学生信息并按总成绩排序;第二个是结合List集合讲解如何运用斗地主案例,探讨TreeMap的有序性在程序优化中的作用。
摘要由CSDN通过智能技术生成

集合案例

一:键盘录入学生案列(TreeMap)

熟练运用TreeMap的排序方式:
需求:键盘录入5个学生信息,并按总成绩从大到小的顺序排序

1.创建一个标准的学生类
  package com.edu01;
public class Student implements Comparable<Student>{
   
    private String name;
    private int chinese;
    private int math;
    private int english;
    private int allScore;
    public Student() {
   
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, int chinese, int math, int english, int allScore) {
   
        super();
        this.name = name;
        this.chinese = chinese;
        this.math = math;
        this.english = english;
        this.allScore = allScore;
    }
    public String getName() {
   
        return name;
    }
    public void setName(String name) {
   
        this.name = name;
    }
    public int getChinese() {
   
        return chinese;
    }
    public void setChinese(int chinese) {
   
        this.chinese = chinese;
    }
    public int getMath() {
   
        return math;
    }
    public void setMath(int math) {
   
        this.math = math;
    }
    public int getEnglish() {
   
        return english;
    }
    public void setEnglish(int english) {
   
        this.english = english;
    }
    public int getAllScore(){
   
        return chinese + math + english;
    }
    @Override
    public int compareTo(Student s) {
   
        // TODO Auto-generated method stub
        int  num = this.getAllScore() - s.getAllScore();
        int num2 = num==0?this.name.compareTo(s.name):num;
        return num2;
    }
}
package com.edu01;
import
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值