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;
    }
}
相关推荐
深盾科技27 分钟前
Kotlin Data Classes 快速上手
android·开发语言·kotlin
zzywxc7871 小时前
详细探讨AI在金融、医疗、教育和制造业四大领域的具体落地案例,并通过代码、流程图、Prompt示例和图表等方式展示这些应用的实际效果。
开发语言·javascript·人工智能·深度学习·金融·prompt·流程图
浮灯Foden1 小时前
算法-每日一题(DAY13)两数之和
开发语言·数据结构·c++·算法·leetcode·面试·散列表
淡海水1 小时前
【原理】Struct 和 Class 辨析
开发语言·c++·c#·struct·class
Q_Q19632884751 小时前
python的电影院座位管理可视化数据分析系统
开发语言·spring boot·python·django·flask·node.js·php
该用户已不存在1 小时前
OpenJDK、Temurin、GraalVM...到底该装哪个?
java·后端
杜子不疼.2 小时前
《Python学习之第三方库:开启无限可能》
开发语言·python·学习
西工程小巴2 小时前
实践笔记-VSCode与IDE同步问题解决指南;程序总是进入中断服务程序。
c语言·算法·嵌入式
TT哇2 小时前
@[TOC](计算机是如何⼯作的) JavaEE==网站开发
java·redis·java-ee
Tina学编程2 小时前
48Days-Day19 | ISBN号,kotori和迷宫,矩阵最长递增路径
java·算法