ArrayList 存学生对象更灵活,因自动扩容且需用泛型;Student 类须重写 equals() 和 hashCode()(仅含 id 等唯一字段);排序用 Comparator 而非 Comparable;输入时注意 nextInt() 后调用 nextLine() 清缓存。

用 ArrayList 存学生对象比用数组更灵活
直接用 Student[] 数组要预先定大小,增删学生时还得手动复制扩容;而 ArrayList 自动处理容量变化,适合学生数量不固定的场景。
关键点:
-
Student类必须有明确的属性(如id、name、score)和合理构造方法 - 避免把
ArrayList声明为ArrayList原生类型,务必用泛型:ArrayList,否则取元素要强转,容易出ClassCastException - 如果后续要按学号查学生,别每次都遍历 —— 可以同步维护一个
HashMap加速查找
增删改查操作别漏掉 equals() 和 hashCode()
当你用 list.remove(student) 或 list.contains(student) 时,Java 默认比较的是对象引用,不是内容。如果两个学生学号姓名都一样,但用 new Student(1,"张三",85) 创建了两次,contains() 会返回 false。
解决办法是重写 Student 类的 equals() 和 hashCode():
立即学习“Java免费学习笔记(深入)”;
public class Student {
private int id;
private String name;
private double score;
// 构造、getter 省略...
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id && Double.compare(student.score, score) == 0 && Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name, score);
}}
IDE(如 IntelliJ)能自动生成,但注意:只把业务上判定“同一个学生”的字段放进 hashCode(),比如通常用 id 就够了,score 变动频繁,不该参与哈希计算。
按成绩排序要用 Comparator,别硬写冒泡
Collections.sort(list) 要求元素实现 Comparable,但学生可能按姓名排、按学号排、按成绩排——硬让 Student 实现某一种 compareTo() 不够通用。
推荐用匿名内部类或 Lambda 写临时比较器:
// 按成绩降序
Collections.sort(students, (s1, s2) -> Double.compare(s2.getScore(), s1.getScore()));
// 按姓名升序(忽略大小写)
Collections.sort(students, Comparator.comparing(Student::getName, String.CASE_INSENSITIVE_ORDER));
注意:Double.compare() 比直接用 s1.getScore() - s2.getScore() 安全,能避免浮点数减法产生 NaN 或溢出问题。
控制台输入学生信息时,Scanner 的换行残留容易导致下一行读空
常见错误写法:
System.out.print("姓名:");
String name = scanner.nextLine(); // ✅ 正确
System.out.print("学号:");
int id = scanner.nextInt(); // ❌ 这里没吃掉回车符
System.out.print("成绩:");
String nextLine = scanner.nextLine(); // ⚠️ 这行直接读到空字符串!解决方式只有两个可靠选择:
- 统一用
nextLine(),数字字段再用Integer.parseInt()转(记得包try-catch) - 在
nextInt()后立刻加一句scanner.nextLine();清掉缓冲区里的换行
别信“scanner.skip("\\R")”这种冷门写法,不同平台换行符不同(\n / \r\n),反而增加不确定性。
实际运行中,最常卡住的地方不是逻辑,而是输入处理和对象相等性判断。哪怕功能写对了,remove() 不生效、contains() 总返回 false、排序结果乱序,八成是这两个环节出了问题。










