• 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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    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());
                }
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    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());
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

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

  • 相关阅读:
    如何写单元测试
    GitHub操作之跨团队操作
    Fastjson反序列化漏洞
    【Leetcode刷题笔记05】242.有效的字母异位词 349. 两个数组的交集 202. 快乐数 1. 两数之和
    Vue3 - 异步组件(写法及详细示例)
    nginx快速入门
    android CountDownTimer倒计时随时随地开启或关闭
    rocketmq各类问题
    【毕业设计】单片机远程wifi红外无接触体温测量系统 - 物联网 stm32
    HTML系统学习
  • 原文地址:https://blog.csdn.net/weixin_51293134/article/details/133774564