java 使用comparator接口对ArrayList排序无效

                 java 使用comparator接口对ArrayList排序无效。

            原因:Comparator返回值在1.7里必须是一对相反数,如1和-1,不能是1和0.因为1.7的排序算法采用timsort,

                   对返回值有严格要求。

                        

                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个比较复杂的问题。首先,你需要定义一个学生类,包括学号、年龄、性别、数学、语文、英语成绩以及平均分等属性。可以按照以下方式定义: ``` public class Student { private String id; private int age; private String gender; private double mathScore; private double chineseScore; private double englishScore; private double averageScore; // 构造函数 public Student(String id, int age, String gender, double mathScore, double chineseScore, double englishScore, double averageScore) { this.id = id; this.age = age; this.gender = gender; this.mathScore = mathScore; this.chineseScore = chineseScore; this.englishScore = englishScore; this.averageScore = averageScore; } // getter、setter方法 public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public double getMathScore() { return mathScore; } public void setMathScore(double mathScore) { this.mathScore = mathScore; } public double getChineseScore() { return chineseScore; } public void setChineseScore(double chineseScore) { this.chineseScore = chineseScore; } public double getEnglishScore() { return englishScore; } public void setEnglishScore(double englishScore) { this.englishScore = englishScore; } public double getAverageScore() { return averageScore; } public void setAverageScore(double averageScore) { this.averageScore = averageScore; } } ``` 接下来,你需要编写一个学生管理类,包含添加、排序、查询、修改和删除学生信息的方法。可以按照以下方式定义: ``` import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner; public class StudentManager { private List<Student> studentList = new ArrayList<Student>(); // 添加学生信息 public void addStudent() { Scanner input = new Scanner(System.in); System.out.println("请输入学生信息:"); System.out.print("学号:"); String id = input.next(); System.out.print("年龄:"); int age = input.nextInt(); System.out.print("性别:"); String gender = input.next(); System.out.print("数学成绩:"); double mathScore = input.nextDouble(); System.out.print("语文成绩:"); double chineseScore = input.nextDouble(); System.out.print("英语成绩:"); double englishScore = input.nextDouble(); double averageScore = (mathScore + chineseScore + englishScore) / 3; // 计算平均分 Student student = new Student(id, age, gender, mathScore, chineseScore, englishScore, averageScore); studentList.add(student); System.out.println("添加成功!"); } // 排序 public void sortStudent() { Collections.sort(studentList, new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { if (o1.getAverageScore() > o2.getAverageScore()) { return -1; } else if (o1.getAverageScore() < o2.getAverageScore()) { return 1; } else { return 0; } } }); System.out.println("排序成功!"); } // 查询学生信息 public void queryStudent() { Scanner input = new Scanner(System.in); System.out.print("请输入要查询的学生学号:"); String id = input.next(); for (Student student : studentList) { if (id.equals(student.getId())) { System.out.println("学号\t年龄\t性别\t数学成绩\t语文成绩\t英语成绩\t平均分"); System.out.println(student.getId() + "\t" + student.getAge() + "\t" + student.getGender() + "\t" + student.getMathScore() + "\t" + student.getChineseScore() + "\t" + student.getEnglishScore() + "\t" + student.getAverageScore()); return; } } System.out.println("未找到该学生!"); } // 修改学生信息 public void modifyStudent() { Scanner input = new Scanner(System.in); System.out.print("请输入要修改的学生学号:"); String id = input.next(); for (Student student : studentList) { if (id.equals(student.getId())) { System.out.println("请输入修改后的学生信息:"); System.out.print("年龄:"); int age = input.nextInt(); System.out.print("性别:"); String gender = input.next(); System.out.print("数学成绩:"); double mathScore = input.nextDouble(); System.out.print("语文成绩:"); double chineseScore = input.nextDouble(); System.out.print("英语成绩:"); double englishScore = input.nextDouble(); double averageScore = (mathScore + chineseScore + englishScore) / 3; // 计算平均分 student.setAge(age); student.setGender(gender); student.setMathScore(mathScore); student.setChineseScore(chineseScore); student.setEnglishScore(englishScore); student.setAverageScore(averageScore); System.out.println("修改成功!"); return; } } System.out.println("未找到该学生!"); } // 删除学生信息 public void deleteStudent() { Scanner input = new Scanner(System.in); System.out.print("请输入要删除的学生学号:"); String id = input.next(); for (Student student : studentList) { if (id.equals(student.getId())) { studentList.remove(student); System.out.println("删除成功!"); return; } } System.out.println("未找到该学生!"); } } ``` 最后,你可以编写一个主函数来测试学生管理系统的功能: ``` public class Main { public static void main(String[] args) { StudentManager studentManager = new StudentManager(); Scanner input = new Scanner(System.in); while (true) { System.out.println("请选择操作:"); System.out.println("1. 添加学生信息"); System.out.println("2. 排序"); System.out.println("3. 查询学生信息"); System.out.println("4. 修改学生信息"); System.out.println("5. 删除学生信息"); System.out.println("0. 退出"); int choice = input.nextInt(); switch (choice) { case 1: studentManager.addStudent(); break; case 2: studentManager.sortStudent(); break; case 3: studentManager.queryStudent(); break; case 4: studentManager.modifyStudent(); break; case 5: studentManager.deleteStudent(); break; case 0: System.exit(0); break; default: System.out.println("无效的选择!"); break; } } } } ``` 希望这个代码可以帮助你完成学生管理系统的编写。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值