这篇文章只有一道题,因为这道练习稍微复杂一点,篇幅较长https://www.bilibili.com/video/BV17F411T7Ao?p=94&vd_source=94fb2114c3889338d33bcb41c3e25538

这里要求5我并没有设置id为String类型,所以要�?5改为:

要求五:查询数组id�?2的学生,如果存在,则将他的年�?+1�?

Students.java

package test5;

//JavaBean文件: Students.java

public class Students {
    private int id;
    private String name;
    private int old;


    public Students() {
    }

    public Students(int id, String name, int old) {
        this.id = id;
        this.name = name;
        this.old = old;
    }

    /**
     * 获取
     * @return id
     */
    public int getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return old
     */
    public int getOld() {
        return old;
    }

    /**
     * 设置
     * @param old
     */
    public void setOld(int old) {
        this.old = old;
    }
}

StudentsTest.java

package test5;

//主入口文�?: StudentsTest.java

import java.util.Scanner;

public class StudentsTest {
    public static void main(String[] args) {
        Students[] arr = new Students[3];

        Scanner sc = new Scanner(System.in);


        //先在数组中存入三个数�?
        Students stu1 = new Students(1, "张三", 24);
        Students stu2 = new Students(2, "李四", 25);
        Students stu3 = new Students(3, "王五", 26);
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;

        //后续要添加的数据
        Students stu4 = new Students();

        System.out.println("请输入您想添加的学生的id:");
        int id = sc.nextInt();
        stu4.setId(id);
        System.out.println("请输入您想添加的学生的姓�?:");
        String name = sc.next();
        stu4.setName(name);
        System.out.println("请输入您想添加的学生的年�?:");
        int old = sc.nextInt();
        stu4.setOld(old);

        boolean flag = contains(arr, stu4.getId());
        if (flag) {
            System.out.println("该id已经存在");
        } else {
            System.out.println("添加成功,添加后数组�?: ");
            int count = getCount(arr);
            if (count == arr.length) {
                //已经存满,需要创建一个新数组
                //并将老数组中元素复制到新数组�?
                Students[] newArr = creatNewArr(arr);
                newArr[count] = stu4;
                printArr(newArr);

                deleteId(newArr);

                changeAge(newArr);


            } else {
                //未存�?,直接将stu4存到索引�?
                arr[count] = stu4;
                printArr(arr);

                deleteId(arr);

                changeAge(arr);


            }
        }
    }

    public static void printArr(Students[] arr) {
        for (int i = 0; i < arr.length; i++) {
            Students stu = arr[i];
            if (stu != null) {
                System.out.println(stu.getId() + ", " + stu.getName() + ", " + stu.getOld());
            }
        }
    }

    //创建一个新数组,并将老数组中元素
    public static Students[] creatNewArr(Students[] arr) {
        Students[] newArr = new Students[arr.length + 1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    //判断数组中已经存了几个元�?
    public static int getCount(Students[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        return count;
    }

    //判断id是否已经存在
    public static boolean contains(Students[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            Students stu = arr[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return true;
                }
            }
        }
        return false;
    }

    public static int getIndex(Students[] arr, int id) {
        for (int i = 0; i < arr.length; i++) {
            Students stu = arr[i];
            if (stu != null) {
                int sid = stu.getId();
                if (sid == id) {
                    return i;
                }
            }
        }
        return -1;
    }
    public static void deleteId(Students[] arr){
        Scanner sc = new Scanner(System.in);
        System.out.println("\n请输入你想删除的id,如无需删除留空即可");
        int id1 = sc.nextInt();
        if (id1 >= 0) {
            int index = getIndex(arr, id1);
            if (index >= 0) {
                arr[index] = null;
                System.out.println("删除成功,删除后信息为:");
                printArr(arr);
            } else {
                System.out.println("id不存在,删除失败");
            }
        }
    }
    public static void changeAge(Students[] arr){
        Scanner sc = new Scanner(System.in);
        System.out.println("\n请输入你想修改的id,如无需修改留空即可");
        int id2 = sc.nextInt();
        if(id2>=0){
            int index = getIndex(arr,id2);
            if(index>=0){
                Students stu = arr[index];
                int age = stu.getOld()+1;
                stu.setOld(age);
                System.out.println("修改"+id2+"号的年龄成功,修改后数组信息为:");
                printArr(arr);
            }else{
                System.out.println("当前id不存�?,修改失败");
            }
        }
    }
}