ArrayList集合demo

Student类:

1.该函数主要实现以下功能:

复制代码
创建一个包含3个Student对象的数组。
给数组添加一个新的Student对象,保持数组中对象的唯一性。
如果新对象的ID已存在于数组中,则不添加。
如果新对象的ID不存在于数组中,则添加到数组中。
当数组已满时,创建一个新的长度加一的数组来存放新对象。
当数组未满时,直接将新对象添加到数组的下一个空位置。
最后打印输出整个数组
java 复制代码
package Programming;

public class StudentTest1 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student s1 = new Student(20180625, "sunshine", 20);
        Student s2 = new Student(20180626, "九色鹿", 21);
        Student s3 = new Student(20180627, "露露水", 22);
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        //要求再添加一个学生对象进入数组
        Student s4 = new Student(20180628, "阳光", 23);
        //唯一性判断
        //已存在 --- 不用添加
        //不存在 === 直接添加
        boolean flag = contains(arr, s4.getId());
        if (flag) {
            //已存在
            System.out.println("ID已存在,不用添加");
        } else {
            //不存在
            //把s4添加到数组中
            //1.数组已经存满 --- 只能创建一个新的数组,新数组长度 = 老数组长度 + 1
            //2.数组没有存满 --- 直接添加
            int count = getCount(arr);
            if (count != arr.length) {
                arr[count] = s4;
                printArr(arr);
            } else {
                Student[] newArr = creatNewArr(arr);
                newArr[count] = s4;
                printArr(newArr);
            }
        }
    }

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

    //创建一个新的数组,长度 = 老数组长度+1
    //然后把老数组拷贝到新数组当中去
    public static Student[] creatNewArr(Student[] arr) {
        Student[] newArr = new Student[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    public static int getCount(Student[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }

    //我要干嘛?
    //我要干这件事情,需要什么才能完成
    //调用处是否需要使用方法的结果?
    public static boolean contains(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() == id) {
                    return true;
                }
            }
        }
        return false;
    }
}

2.该函数主要实现以下功能:

复制代码
创建一个包含3个Student对象的数组。
初始化数组中的Student对象。
通过调用getIndex方法,查找数组中是否存在指定id的学生对象,并返回其索引。
如果存在,则将该学生对象置为null,并通过调用printArr方法遍历打印数组。
如果不存在,则输出提示信息
java 复制代码
package Programming;

public class StudentTest2 {
    public static void main(String[] args) {
        Student[] arr = new Student[3];
        Student s1 = new Student(20180625, "sunshine", 20);
        Student s2 = new Student(20180626, "九色鹿", 21);
        Student s3 = new Student(20180627, "露露水", 22);
        arr[0] = s1;
        arr[1] = s2;
        arr[2] = s3;

        int index = getIndex(arr, 20180626);
        System.out.println(index);

        if (index >= 0) {
            arr[index] = null;
            //遍历数组
            printArr(arr);
        } else {
            //删除的内容不存在
            System.out.println("你要删除的id不存在,请修改id");
        }
    }

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

    //找到ID在数组中的索引
    public static int getIndex(Student[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                if (arr[i].getId() == id) {
                    return i;
                }
            }
        }
        //当循环结束之后,还没有找到,就表示不存在。
        return -1;
    }
}
相关推荐
IKUN家族1 小时前
Spring MVC(一)
java·spring·mvc
imuliuliang2 小时前
关于数据结构在算法设计中的核心作用解析7
算法
老马识途2.03 小时前
关于跨域问题的总结
java·前端
魔力女仆3 小时前
分享一个 JS 鼠标跟随贪吃蛇背景库
开发语言·javascript·计算机外设
都叫我大帅哥3 小时前
Java日期时间三十年战争:从Date考古到LocalDateTime革命,以及数据库与前端的那点事儿
java
Muscleheng3 小时前
SpringBoot 集成 DeepSeek 实现 RAG 文档问答
java·spring boot·ai·springai
2401_894915534 小时前
GEO 搜索优化完整源码从零部署:环境配置、集群搭建全流程
开发语言·python·tcp/ip·算法·unity
2501_936415694 小时前
可变参数&综合练习&斗地主游戏
java·windows·游戏
麻瓜老宋4 小时前
AI开发C语言应用按步走,表达式计算器calc的第二十二步,分号赋值链式修复、TOKEN_ASSIGN
c语言·开发语言·atomcode