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;
    }
}
相关推荐
小叶学C++4 分钟前
【C++】类与对象(下)
java·开发语言·c++
ac-er88885 分钟前
PHP“===”的意义
开发语言·php
2401_854391088 分钟前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Funny_AI_LAB15 分钟前
MetaAI最新开源Llama3.2亮点及使用指南
算法·计算机视觉·语言模型·llama·facebook
Cikiss16 分钟前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
wxin_VXbishe17 分钟前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
Cikiss18 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
NuyoahC22 分钟前
算法笔记(十一)——优先级队列(堆)
c++·笔记·算法·优先级队列
jk_10124 分钟前
MATLAB中decomposition函数用法
开发语言·算法·matlab
weixin_4640780725 分钟前
C#串口温度读取
开发语言·c#