集合案例
一:键盘录入学生案列(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