未分类
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;
}
}
}
- 本页地址 https://www.9713job.com/?p=2106
- 上一篇 <<电脑教程:如何解决mysql上传限制
- 下一篇 >>2020java教程:内部类与常用类



发表回复