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;
    }
}
相关推荐
moxiaoran57539 分钟前
Go语言的错误处理
开发语言·后端·golang
ballball~~11 分钟前
拉普拉斯金字塔
算法·机器学习
Cemtery11611 分钟前
Day26 常见的降维算法
人工智能·python·算法·机器学习
yugi98783844 分钟前
MATLAB的多层感知器(MLP)与极限学习机(ELM)实现
开发语言·matlab
heartbeat..1 小时前
数据库基础知识体系:概念、约束、范式与国产产品
java·数据库·学习笔记·国产数据库
Never_Satisfied1 小时前
C#获取汉字拼音字母方法总结
开发语言·c#
zh_xuan1 小时前
kotlin 密封类
开发语言·kotlin
PXM的算法星球1 小时前
【操作系统】哲学家就餐问题实现详解
java
2301_815357702 小时前
Java项目架构从单体架构到微服务架构的发展演变
java·微服务·架构
码小猿的CPP工坊2 小时前
C++软件开发之内存泄漏闭坑方法
开发语言·c++