import java.util.Arrays; import java.util.Comparator;

public class Demo04 { private static Student[] stus = new Student[6];

static {
    stus[0] = new Student("zhangsan", 17, 99, 77);
    stus[1] = new Student("lisi", 21, 85, 86);
    stus[2] = new Student("wangwu", 20, 75, 94);
    stus[3] = new Student("zhaoliu", 17, 75, 95);
    stus[4] = new Student("qianqi", 19, 66, 44);
    stus[5] = new Student("yanzu", 18, 75, 86);
}

public static void main(String[] args) {
    //3、通过比较器排序完成:按照年龄升序排列,年龄相同比姓名
    Arrays.sort(stus, new Comparator<Student>() {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.getAge() != o2.getAge()) {
                return o1.getAge() - o2.getAge();
            } else {
                return o1.getName().compareTo(o2.getName());
            }
        }
    });

    //2、遍历集合,打印对象属性查看结果
    for (Student stu : stus) {
        System.out.println(stu);
    }
}

}

//1、通过自然排序完成:按照语文成绩升序排列,语文成绩相同,则按继续比英语成绩(注释掉再写比较器排序) //class Student implements Comparable{ // private String name; // private int age; // private int chinese; // private int english; // // public Student() { // } // // public Student(String name, int age, int chinese, int english) { // this.name = name; // this.age = age; // this.chinese = chinese; // this.english = english; // } // // public String getName() { // return name; // } // // public void setName(String name) { // this.name = name; // } // // public int getAge() { // return age; // } // // public void setAge(int age) { // this.age = age; // } // // public int getChinese() { // return chinese; // } // // public void setChinese(int chinese) { // this.chinese = chinese; // } // // public int getEnglish() { // return english; // } // // public void setEnglish(int english) { // this.english = english; // } // // @Override // public String toString() { // return "Student{" + // "name='" + name + ''' + // ", age=" + age + // ", chinese=" + chinese + // ", english=" + english + // '}'; // } // // @Override // public int compareTo(Student o) { // if (this.chinese != o.chinese) { // return this.chinese - o.chinese; // } else { // return this.english - o.english; // } // } //}

class Student{ private String name; private int age; private int chinese; private int english;

public Student() {
}

public Student(String name, int age, int chinese, int english) {
    this.name = name;
    this.age = age;
    this.chinese = chinese;
    this.english = english;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int getChinese() {
    return chinese;
}

public void setChinese(int chinese) {
    this.chinese = chinese;
}

public int getEnglish() {
    return english;
}

public void setEnglish(int english) {
    this.english = english;
}

@Override
public String toString() {
    return "Student{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", chinese=" + chinese +
            ", english=" + english +
            '}';
}
04、分析以下需求并完成代码 阅读代码通过自然排序完成:按照语文成绩升序排列语文成绩相同则按继续比英语成绩 将自然排序相关代码注释然后通过比较器排序完成:按照年龄升序排列年龄相同比姓名 遍历集合打印对象属性查看结果public class Demo04 private static Student stus = new Student6; s

原文地址: https://www.cveoy.top/t/topic/ck3P 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录