未分类
2020-08-17 14:08:18
1822677238@qq.com
手机扫码查看
接口实现学生数组排序
import java.util.Arrays;
public class demos {
public static void main(String[] args) {
Student[] stu={
new Student("张三",22,82),
new Student("李四",25,90),
new Student("王五",26,89)
};
Arrays.sort(stu);
for(int i=0;i<stu.length;i++){
System.out.println(stu[i].name+"\t"+stu[i].age+"\t"+stu[i].score);
}
}
}
class Student implements Comparable<Student>{
String name;
int age;
double score;
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public int compareTo(Student s) {
if(this.score>s.score){
return -1;
}else if(this.score<s.score){
return 1;
}else{
return this.age-s.age;
}
}
}



发表回复