数组对象练习

定义一个长度为3的数组,数组存储1~3名学生对象作为初始数据,学生对象的学号,姓名各不相同。学生的属性:学号,姓名,年龄。

要求1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。

要求2:添加完毕之后,遍历所有学生信息。

要求3∶通过id删除学生信息如果存在,则删除,如果不存在,则提示删除失败。

要求4:删除完毕之后,遍历所有学生信息。

要求5:查询数组id为“02”的学生,如果存在,则将他的年龄+1岁
 

题目拆解来看,先看要求1和要求2

package Test7;

public class StudentTest {
    public static void main(String[] args) {

        //创建长度三的数组
        Student[] arr = new Student[3];

        //创建学生对象
        Student s1 = new Student(1,"小明",19);
        Student s2 = new Student(2,"小红",20);
        //Student s3 = new Student(3,"小李",21);

        //把学生对象添加到数组当中
        arr[0] = s1;
        arr[1] = s2;
        //arr[2] = s3;

        //再次创建一个学生对象
        Student s4 = new Student(4,"小白",22);
        //唯一性判断
        //己存在---不用添加
        //不存在---就可以把学生对象添加进数组
        boolean flag = contains(arr,s4.getId());
        if(flag){
            System.out.println("当前id重复,请重新输入");
        }else {
            //不存在---就可以把学生对象添加进数组/把stu4添加到数组当中
            //1.数组已经存满---只能创建一个新的数组,新数组的长度=老数组+11
            // 2.数组没有存满---直接添加
            int newLength = countStu(arr);
            if(newLength == arr.length){
                //已存满,需要自己添加新数组,数组长度为旧数组 + 1
                Student[] newArr = addNewArr(arr);
                newArr[newLength] = s4;
                printArr(newArr);
            }else {
                //未存满,直接添加
                arr[newLength] = s4;
                printArr(arr);
            }
        }


    }
    //打印数组中存在的元素
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                System.out.println(stu.getId()+", "+stu.getName()+", "+stu.getAge());
            }
        }
    }

    //创建新数组,长度为旧数组长度 + 1
    public static Student[] addNewArr(Student[] arr){
        //创建新数组
        Student[] newArr = new Student[arr.length + 1];
        //将老数组的元素添加到新数组中
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }


    //判断插入学生的id是否在数组中
    public static boolean contains(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            //依次获取数组里面每一个学生对象
            Student stu = arr[i];
            //获取数组中学生的id
            int sid = stu.getId();
            if (sid == id){
                return true;
            }
        }
        return false;
    }

    //获取数组中有数据的个数
    public static int countStu(Student[] arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Student stu = arr[i];
            if(stu != null){
                sum++;
            }
        }
        return sum;
    }
}

下面是类的写法

package Test7;

public class Student {
    private int id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值