Java学习复杂的对象数组操作

Java学习复杂的对象数组操作

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

  • 要求1:再次添加一个学生对象,并在添加的时候进行学* 号的唯一性判断。
  • 要求2:添加完毕之后,遍历所有学生信息。
  • 要求3:通过id删除学生信息
    ​ 如果存在,则删除,如果不存在,则提示删除失败。
  • 要求4:删除完毕之后,遍历所有学生信息。
  • 要求5:查询数组id为“heima002”的学生,如果存在,则将他的年龄+1岁
package test;

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

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

package test;

public class Test {
    public static void main(String[] args) {
        Student[] a = new Student[3];
        Student stu1 = new Student(1,"zhangsan",23);
        Student stu2 = new Student(2,"lisi",24);
        a[0] = stu1;
        a[1] = stu2;
        Student stu4 = new Student(3,"linda",25);

        boolean flag = contains(a,stu4.getId());
        if(flag){
            System.out.println("当前id值重复,请修改后重试!");
        }else{
            int count = getCount(a);
            if (count == a.length) {
                Student[] newarr = creatNewarr(a);
                newarr[count] = stu4;
                printArr(newarr);
            }else{
                a[count] = stu4;
                printArr(a);
            }
        }

    }
    public static boolean contains(Student[] a,int id){
        for (int i = 0; i < a.length; i++) {
            Student stu = a[i];
            if(stu != null){
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }

        }
        return false;
    }
    public static  int getCount(Student[] a){
        int count = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != null) {
                count++;
            }
        }
        return  count;
    }
    public static Student[] creatNewarr(Student[] a){
        Student[] newarr = new Student[a.length+1];
        for (int i = 0; i < a.length; i++) {
            newarr[i] = a[i];
        }
        return newarr;
    }
    public static void printArr(Student[] a){
        for (int i = 0; i < a.length; i++) {
            Student stu = a[i];
            if(stu != null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }
        }

    }
}

package test;

public class Test1 {
    public static void main(String[] args) {
        Student[] b =new Student[3];
        Student stu1 = new Student(1, "zhangsan", 23);
        Student stu2 = new Student(2, "lisi", 24);
        Student stu3 = new Student(3, "wangwu", 25);

        b[0] = stu1;
        b[1] = stu2;
        b[2] = stu3;
        int index = getId(b,5);
        if( index >= 0){
            b[index] = null;
        }else {
            System.out.println("所删除的id不存在,删除失败!");
        }
    }
    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());
            }
        }
    }
    public static  int getId(Student[] b,int id){
        for (int i = 0; i < b.length; i++) {
            Student stu = b[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return  i;
                }
            }
        }
        return -1;
    }
}

package test;

public class Test2 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];

        Student stu1 = new Student(1, "zhangsan", 23);
        Student stu2 = new Student(2, "lisi", 24);
        Student stu3 = new Student(3, "wangwu", 25);

        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;

        int index = getIndex(arr, 5);
        if(index >= 0){
            //存在, 则将他的年龄+1岁
            Student stu = arr[index];
            //把原来的年龄拿出来
            int newAge = stu.getAge() + 1;
            //把+1之后的年龄塞回去
            stu.setAge(newAge);
            //遍历数组
            printArr(arr);
        }else{
            //不存在,则直接提示
            System.out.println("当前id不存在,修改失败!");
        }

    }
    public static int getIndex(Student[] arr , int id){
        for (int i = 0; i < arr.length; i++) {
            //依次得到每一个学生对象
            Student stu = arr[i];
            //对stu进行一个非空判断
            if(stu != null){
                int sid = stu.getId();
                if(sid == id){
                    return i;
                }
            }
        }

        //当循环结束之后,还没有找到就表示不存在
        return -1;
    }

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

今天先记录到这里了!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

加油吧少年时代

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值