public class StudenManager {
private Student[] students;
public static int length = 10;// 初始数组长度
public StudenManager() {// 默认构造
this.students = new Student[length];
for (int i = 0; i < 10; i++) {
this.students[i] = new Student();
}
}
public void setStudent(int i, String name, String stuNo, String birthday) {
// 初始化
this.students[i - 1].setters(name, stuNo, birthday);
}
public void printlength() {// 输出学生数组长度
System.out.println(students.length);
}
public void add(int i) {// 增加学生数组数量
Student[] copystudents = new Student[students.length + i];
System.arraycopy(students, 0, copystudents, 0, students.length);
students = copystudents;
length += i;
}
public void remove(int remove) {// 移除学生
students[remove - 1] = null;
if (remove < length) {
for (int i = remove; i < length; i++) {
students[i - 1] = students[i];
}
students[length - 1] = null;
Student[] copystudents = new Student[students.length - 1];
System.arraycopy(students, 0, copystudents, 0, students.length - 1);
students = copystudents;
}
length--;
}
public Student searchByStuNo(String number) {// 学号查找
for (int i = 0; i < students.length; i++) {
if (students[i].getstuNo().equals(number)) {
return students[i];
}
}
return null;
}
public void printlnfo(int num) {// 输出学生信息
if ((num > 0) && (num < students.length) && students[num] != null) {
students[num - 1].getters();
} else {
System.out.println("The student of " + num + " is null");
}
}
public void printlnfo() {// 输出所有学生信息
for (int i = 0; i < length; i++) {
if (students[i] != null) {
students[i].getters();
} else {
System.out.println("The student of " + (i + 1) + " is null");
}
}
}
public Student getstudent(int num) {// 得到学生对象
return this.students[num];
}
public static void main(String[] args) {
StudenManager s1 = new StudenManager();
Student s2 = new Student();
s1.setStudent(1, "zhang", "165156165165", "2009-12-16");
s1.setStudent(2, "liu", "34343345", "2005-12-16");
s1.setStudent(3, "zhang", "34534", "2004-12-16");
s1.setStudent(4, "zhang", "0304245345320127", "2008-12-16");
s1.setStudent(5, "zhang", "5453", "2008-12-16");
s1.setStudent(6, "zhang", "0304223530129", "2007-12-16");
s1.setStudent(7, "zhang", "03054225350130", "2001-12-16");
s1.setStudent(8, "zhang", "03045220131", "2000-12-16");
s1.setStudent(9, "zhang", "0354552520132", "2004-12-16");
s1.setStudent(10, "zhang", "03045220133", "2004-12-16");
s1.add(3);// 增
s1.printlength();
s1.printlnfo();
s1.remove(2);// 删
s1.printlength();
s1.printlnfo();
s1.setStudent(1, "wang", "0304220125", "2004-11-12");
s1.printlength();// 改
s1.printlnfo();
s2 = s1.searchByStuNo("0304220125");// 查
s2.getters();
s2 = s1.getstudent(3);// 查
s2.getters();
}
}
public class Student {
private String name;
private String stuNo;
private String birthday;
public Student() {
}
public void setters(String name, String stuNo, String birthday) {
this.name = name;
this.stuNo = stuNo;
this.birthday = birthday;
}
public void getters() {
System.out.println("姓名:" + name + " 学号:" + stuNo + " 生日:" + birthday);
}
public String getstuNo(){
return this.stuNo;
}
}