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;
    }
}
相关推荐
hjyowl5 分钟前
题解:AT_abc407_c [ABC407C] Security 2
c语言·开发语言·算法
唐墨1237 分钟前
android与Qt类比
android·开发语言·qt
书中自有妍如玉8 分钟前
.net 使用MQTT订阅消息
java·前端·.net
风铃儿~33 分钟前
Spring AI 入门:Java 开发者的生成式 AI 实践之路
java·人工智能·spring
Code_流苏34 分钟前
C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
开发语言·c++·stl容器·课设·期末大作业·日历程序·面向对象设计
斯普信专业组39 分钟前
Tomcat全方位监控实施方案指南
java·tomcat
忆雾屿1 小时前
云原生时代 Kafka 深度实践:06原理剖析与源码解读
java·后端·云原生·kafka
道剑剑非道1 小时前
QT开发技术【ffmpeg + QAudioOutput】音乐播放器 完善
开发语言·qt·ffmpeg
武昌库里写JAVA1 小时前
iview Switch Tabs TabPane 使用提示Maximum call stack size exceeded堆栈溢出
java·开发语言·spring boot·学习·课程设计