编写StudenManager类,用于增加、删除、查找和修改学生对象。它至少具有私有属性students(Student数组类型,用于保存学生)、public方法add(增加学生)、remove(根

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;
    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值