java之面向对象练习2

题目:

java之面向对象练习2_第1张图片

public class studentTest1 {
    public static void main(String[] args) {
        //1,创建学生数组
        student[] stu = new student[2];
        student s1 = new student(1,"小明",12);
        student s2 = new student(2,"小花",15);
        stu[0] = s1;
        stu[1] = s2;
        //2,判断学号唯一性
        boolean flag = contain(stu,3);
        //3,插入
        insert(flag,stu);
        //4,查id删除学生信息
        delete(stu,1);


    }
    public static void insert(boolean flag,student[] stu){
        if(flag)
        {
            System.out.println("id 重复,请重新输入id");
        }
        else{
            //判满
            if(stu.length==getCount(stu)){
                student[] newStu = new student[stu.length + 1];
                for (int i = 0; i < stu.length; i++) {//范围是stu的长度,不是newStu的长度,否则出现越界提醒
                    newStu[i]=stu[i];
                }
                student s3 = new student(3,"小红",16);
                newStu[getCount(stu)]=s3;
                stu = newStu;
                for (int i = 0; i < stu.length; i++) {
                    student s = stu[i];
                    System.out.println(s.getId()+","+s.getName()+","+s.getAge());
                }
            }
            else{
                student s3 = new student(3,"小红",16);
                stu[getCount(stu)] = s3;
                for (int i = 0; i < stu.length; i++) {
                    student s = stu[i];
                    System.out.println(s.getId()+","+s.getName()+","+s.getAge());
                }
            }
        }
    }
    public static void delete(student[] stu,int id) {
        //查询是否id存在
        int index = getIndex(stu, id);
        if (index >= 0) {
            stu[index] = null;
        } else {
            System.out.println("删除失败");
        }
        for (int i = 0; i < stu.length; i++) {
            student s = stu[i];
            if (stu[i] != null) {
                System.out.println(s.getId() + "," + s.getName());
            }

        }
    }
        public static int getIndex(student[] stu ,int id){
            for (int i = 0; i 

你可能感兴趣的:(java,开发语言)