Comparabe比较器排序

class Student implements Comparable { // 指定类型为Student
private String name ;
private int age ;
private float score ;
public Student(String name,int age,float score){
this.name = name ;
this.age = age ;
this.score = score ;
}
public String toString(){
return name + “\t\t” + this.age + “\t\t” + this.score ;
}
public int compareTo(Student stu){ // 覆写compareTo()方法,实现排序规则的应用
if(this.score>stu.score){
return -1 ;
}else if(this.score<stu.score){
return 1 ;
}else{
if(this.age>stu.age){
return 1 ;
}else if(this.age<stu.age){
return -1 ;
}else{
return 0 ;
}
}
}
};
public class ComparableDemo01{
public static void main(String args[]){
Student stu[] = {new Student(“张三”,20,90.0f),
new Student(“李四”,22,90.0f),new Student(“王五”,20,99.0f),
new Student(“赵六”,20,70.0f),new Student(“孙七”,22,100.0f)} ;
java.util.Arrays.sort(stu) ; // 进行排序操作
for(int i=0;i<stu.length;i++){ // 循环输出数组中的内容
System.out.println(stu[i]) ;
}
}
};

全部评论

相关推荐

2025-12-19 19:02
西安交通大学 Java
程序员牛肉:双九,而且还是西交这种比较好的985九没必要再投日常了。你投中小厂,人家会觉得你学历这么顶还面试肯定是海投的,过了你也不去。所以不约你了。 直接准备暑期实习就好,现在你可以面试。但是目的不再是去日常实习了,而是熟悉面试节奏。 后续把精力放到八股,算法和AI知识上。抽空把自己这两个项目换了,怎么选项目可以看看我主页写的文章。 你学历不错的,不要焦虑
那些拿到大厂offer的...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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