海阔天空

当前时间为:
欢迎大家来到海阔天空https://www.9713job.com,广告合作以及淘宝商家推广请微信联系15357240395

2020java教程:接口实现学生数组排序

未分类
2020-08-17 14:08:18
1822677238@qq.com

手机扫码查看

2020java教程:接口实现学生数组排序

接口实现学生数组排序

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;
        }
    }
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注