public class Main {
public static void main(String[] args) {
student[] students = {
new student("heima1001", "张三", 18),
new student("heima1002", "李四", 19),
new student("heima1003", "王五", 20)
};
student.addStudent(students, "heima1004", "赵六", 21);
student.printStudents(students);
student.removeStudentByID(students, "heima1002");
student.printStudents(students);
student.updateStudentAge(students, "heima1001", 19);
student.printStudents(students);
}
}
public class student {
private String id;
private String name;
private int age;
public student(){}
public student(String id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void addStudent(student[] students, String id, String name, int age) {
for (student s : students) {
if (s.getId().equals(id)) {
System.out.println("学号已存在,无法添加");
return;
}
}
students[students.length] = new student(id, name, age);
}
public static void printStudents(student[] students) {
System.out.println("学生信息:");
for (student s : students) {
System.out.printf("学号: %s, 姓名: %s, 年龄: %d%n", s.getId(), s.getName(), s.getAge());
}
}
public static boolean removeStudentByID(student[] students, String id) {
for (int i = 0; i < students.length; i++) {
if (students[i].getId().equals(id)) {
System.arraycopy(students, i + 1, students, i, students.length - i - 1);
students[students.length - 1] = null;
return true;
}
}
System.out.println("找不到该学生,删除失败");
return false;
}
public static void updateStudentAge(student[] students, String id, int newAge) {
for (student s : students) {
if (s.getId().equals(id)) {
s.setAge(newAge);
break;
}
}
}
}

为什么会出现这个错误呢?这个需要怎么改正呀?