• 黑马Java笔记第九讲—ArrayList集合和学生管理系统


    🎨 个人介绍

    👉大家好,我是:知识的搬运工旺仔

    👉认真分享技术,记录学习过程的点滴,如果我的分享能为你带来帮助,请支持我奥🍻

    👉你的支持,是我每天更新的动力。

    👉赞点:👍 留言:✍ 收藏:⭐

    👉个人格言:想法一步一步的落实,才是你我前进最佳选择。

    1. ArrayList

    1.1 ArrayList类概述、

    1 )什么是集合

    提供一种存储空间的存储模型,储存的数据容量可以发生改变

    2 )ArrayList集合的特点

    底层是数组实现,长度可以变化

    3 )泛型的使用

    用于约束集合中存储元素的数据类型

    1.2 ArrayList类的常用方法

    1 )构造方法

     public ArrayList();   //创建一个空的集合
    
    • 1

    2 )成员方法

    方法名说明
    public boolean remove(Object o)删除指定的元素,返回删除是否成功
    public E remove(int index)删除指定索引处的元素,返回删除的元素
    public E set(int index,E element)修改指定索引处的元素
    public E get(int index)返回指定索引处的元素
    public int size()返回集合中的元素的个数
    public boolean add(E e)将指定的元素追加到此集合的末尾
    public void add(int index,E element)在此集合中的指定位置插入指定元素

    3 )代码实例

    public class ArrayDemo {
        public static void main(String args) {
            // 创建集合
            ArrayList<String> array = new ArrayList<String>();
            
            //添加元素
            array.add("hello");
            array.add("world");
            array.add("java");
            
            //删除元素的练习
            System.out.println(array.remove("world"));
            System.out.println(array.remove("java"));
            // 指定索引处删除元素
            System.out.println(array.remove(2));
            // 出现IndexOutBoundsException异常
            System.out.println(array.remove(3));
            
            //修改指定索引的元素
             System.out.println(array.set(1,"javaee"));
            // 出现IndexOutBoundsException异常
             System.out.println(array.set(3,"javaee"));
            
            //返回指定索引处的元素
            System.out.println(array.get(0));
            System.out.println(array.get(1));
            System.out.println(array.get(2));
            // 出现IndexOutBoundsException异常
            System.out.println(array.get(3));
            
            // 输出集合元素的个数
            System.out.println(array.size());
            
            //输出集合
            System.out.println(array);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    1.3 ArrayList存储字符串并遍历

    1.3.1 案例需求

    创建一个存储的字符串的集合,存储3个字符串元素,使用程序实现在控制台遍历该集合

    1.3.2 思路分析

    1. 创建一个集合对象
    2. 往集合对象里边添加字符串对象
    3. 遍历集合,首先要能够获取到集合的每一个元素,这个通过get(int index) 方法实现
    4. 遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现
    5. 遍历集合的通用格式

    1.3.3 代码实现

    for循环版本

     public static void main(String[] args) {
         //创建集合对象
         ArrayList<String> array = new ArrayList<>();//往集合对象里边添加字符串
         array.add("刘正风");
         array.add("左冷禅");
         array.add("风清扬");//获取集合的长度
         System.out.println(array.size());//遍历集合的通过格式
         for (int i = 0; i < array.size(); i++) {
             String s = array.get(i);
             System.out.println(s);
         }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    增强for循环

     public static void main(String[] args) {
         //创建集合对象
         ArrayList<String> array = new ArrayList<>();//往集合对象里边添加字符串
         array.add("刘正风");
         array.add("左冷禅");
         array.add("风清扬");//获取集合的长度
         System.out.println(array.size());//遍历集合的通过格式
         for (String s : array) {
             System.out.println(s);
         }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    1.4 ArrayList 存储学生对象并遍历

    1.4.1 案例需求

    创建一个存储学生对象的集合,存储3个学生对象,使用程序来实现控制台遍历该集合

    1.4.2 思路分析

    1. 定义一个学生类
    2. 创建一个集合对象
    3. 创建学生对象
    4. 添加学生对象到集合中
    5. 遍历集合,采用通用遍历格式实现

    1.4.3 代码分析

    1 )正常思路

    public class ArrayListTest {
        public static void main(String args[]) {
            //创建集合对象
            ArrayList<Student> array = new ArrayList<> ();
            
            //创建学生对象
            Student s1 = new Student("风清扬"30);
            Student s1 = new Student("林青霞"32);
            Student s1 = new Student("张曼玉"25);
            
            //添加学生对象
            array.add(s1);
            array.add(s2);
            array.add(s3);
            
            // 遍历集合,采用通用遍历格式是实现
            for(int i = 0; i <array.size(); i++) {
                Student s = array.get(i);
                System.out.println(s.getName() + "," + s.getAge())
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    2 )改进的思路

    public class ArrayListTest {
        public static void main(String[] args) {
            // 创建集合对象
            ArrayList<Student> array = new ArrayList<Student>();
            
            //为了提高代码的复用性,我们用方法改进程序
            addStudent(array);
            addStudent(array);
            addStudent(array);
            
            // 遍历集合,采用通用遍历格式是实现
            for(int i = 0; i <array.size(); i++) {
                Student s = array.get(i);
                System.out.println(s.getName() + "," + s.getAge())
            }  
        }
        
        public static void addStudent(ArrayList<Student> array) {
            //键盘录入学生对象所需要的数据
            Scanner input = new Scanner(System.in);
            
            System.out.println("请输入学生姓名");
            String name = input.naxtLine();
            
            System.out.println("请输入学生年龄");
            String age = input.naxtLine();
            
            //创建学生对象,并给学生对象赋值
            Student s = new Student();
            s.setName(name);
            s.setAge(age);
            
            //往集合里边添加对象
            array.add(s);
        }
    } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    2. 学生管理系统

    1.学生类

    1 )学生类

    1. 成员变量

      学号:Sid

      姓名:name

      年龄:age

      居住地:address

    2. 构造方法

      无参构造

      有四个参数的构造

    3. 成员方法

      每个成员变量的get/set方法

    /**
     * @author 86131
     */
    public class Student {
        //学号
        private String sid;
        //姓名
        private String name;
        //年龄
        private String age;
        //居住地
        private String address;//无参构造器
        public Student() {
        }//有参构造器
        public Student(String sid, String name, String age, String address) {
            this.sid = sid;
            this.name = name;
            this.age = age;
            this.address = address;
        }//get和set方法
    ​
    ​
        public String getSid() {
            return sid;
        }public void setSid(String sid) {
            this.sid = sid;
        }public String getName() {
            return name;
        }public void setName(String name) {
            this.name = name;
        }public String getAge() {
            return age;
        }public void setAge(String age) {
            this.age = age;
        }public String getAddress() {
            return address;
        }public void setAddress(String address) {
            this.address = address;
        }
    }
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    2. 主界面的实现

    思路:

    1. 用输出语句完成主界面的编写
    2. 用Scanner实现键盘录入数据
    3. 用switch语句完成操作的选择
    4. 用循环完成再次回到主界面
    public static void main(String[] args) {
            //使用循环完成再次回到主界面
            while (true) {
                //用输出语句完成主界面的编写
                System.out.println("---------欢迎来到学生管理系统---------");
                System.out.println("1.添加学生");
                System.out.println("2.删除学生");
                System.out.println("3.修改学生");
                System.out.println("4.查看所有学生");
                System.out.println("5.退出");
                System.out.println("请输入你的选择:");//用Scanner实现键盘录入
                Scanner input = new Scanner(System.in);
                String line = input.nextLine();//用switch语句完成操作的选择
                switch (line) {
                    case "1" :
                        System.out.println("添加学生");
                        break;
                    case "2" :
                        System.out.println("删除学生");
                        break;
                    case "3" :
                        System.out.println("修改学生");
                        break;
                    case "4" :
                        System.out.println("查看所有学生");
                        break;
                    case "5" :
                        System.out.println("谢谢使用");
                        System.exit(0);
                    default :
                        System.out.println("你输入的有误");
                        break;
                }
            }}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    3. 添加学生的代码书写

    思路:

    1. 用键盘录入选择添加学生

    2. 定义一个方法,用于添加学生

      • 显示提示信息,提示要输入何种信息
      • 键盘录入学生对象所需要的数据
      • 创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
      • 将学生对象添加到集合中
      • 给处添加成功提示
    3. 调用方法

    /**
         * 定义一个方法,用于添加学生信息
         */
        public static void addStudent(ArrayList<Student> arrayStudent) {
            //键盘录入学生的信息
            Scanner input = new Scanner(System.in);
    
            //为了让sId在while循环外面被访问到,我们就把它定义在循环外
            String sId;
            // 为了让程序回到这里,使用while循环
            while (true) {
                System.out.println("请输入学生的学号");
                sId = input.nextLine();
                boolean flag = isUsedOrExists(arrayStudent, sId);
                if (flag) {
                    System.out.println("你输入的学号已经被使用,请重新输入");
                } else {
                    break;
                }
            }
            System.out.println("请输入学生的姓名");
            String name = input.nextLine();
            System.out.println("请输入学生的年龄");
            String age = input.nextLine();
            System.out.println("请输入学生的居住地");
            String address = input.nextLine();
    
            //创建学生对象
            Student s = new Student();
            s.setName(name);
            s.setAge(age);
            s.setAddress(address);
            s.setsId(sId);
    
            //将学生对象添加到集合中
            arrayStudent.add(s);
            //给出提示
            System.out.println("添加学生成功");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    4. 查看学生的代码编写

    思路:

    1. 用键盘录入选择查看所有学生信息

    2. 定义一个方法,用于查看学生信息

      • 显示表头信息
      • 将集合中数据按照对应格式显示学生信息,年龄显示补充“岁”
    3. 调用方法

    注意:用return 方法将不会在执行

     /**
         * 查看学生信息
         */
        public static void findStudent(ArrayList<Student> arrayStudent) {
            //判断集合是否有数据
            if (arrayStudent == null) {
                System.out.println("无信息,请先添加信息");
                //为了让程序不在往下执行,给出return
                return;
            }
            //显示表头信息
            System.out.println("学号\t\t 姓名\t年龄\t\t居住地");
    
            //将集合中数据取出按照格式显示
            for (Student s : arrayStudent) {
                System.out.println(s.getsId() + "\t" + s.getName() + "\t" + s.getAge() + "\t\t" + s.getAddress());
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5.删除学上的代码编写

    1 )思路:

    1. 用键盘录入选择删除学生信息

    2. 定义一个方法,用于删除学生信息

      • 显示提示信息
      • 键盘录入要删除的学生的学号
      • 遍历集合将对应学生对象从集合中删除
      • 给出删除成功提示
    3. 调用方法

    2 )遍历删除集合的元素方法

    1. 第一种
    int index = -1;
            for (int i =0 ; i<arrayStudent.size(); i++) {
                Student s = arrayStudent.get(i);
                if (s.getsId().equals(id)) {
                    arrayStudent.remove(i);
                    index = i;
                    break;
                }
            }
            if (index == -1) {
                System.out.println("删除失败,没有此学生");
            }else {
                System.out.println("删除学生成功");
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 第二种
    int index = -1;
    
            for (Student s : arrayStudent) {
                if (s.getsId().equals(id)) {
                    arrayStudent.remove(s);
                    index =1;
                    break;
                }
            }
            
            if (index == 1) {
                System.out.println("删除学生成功");
            }else {
                System.out.println("删除失败,没有此学生");
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 第三种
    boolean isDelete = arrayStudent.removeIf(s -> s.getsId().equals(id));
    
            if (isDelete){
                //给出删除学生的提示
                System.out.println("删除学生成功");
            }else {
                System.out.println("删除失败,没有此学生");
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3 )所有代码

     /**
         * 删除学生信息
     */
    public static void deleteStudent(ArrayList<Student> arrayStudent) {
            //键盘录入要删除的学生学号
            Scanner input = new Scanner(System.in);
    
            System.out.println("请输入你要删除的学生的学号");
            String id = input.nextLine();
    
            //遍历集合,将对应学生对象删除
    
            arrayStudent.removeIf(s -> s.getsId().equals(id));
    
    
            //给出删除学生的提示
            System.out.println("删除学生成功");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    6. 修改学生的代码

    思路:

    1. 用键盘录入选择修改学生信息

    2. 定义一个方法,用于修改学生信息

      • 显示提示信息
      • 键盘录入要修改的学生学号
      • 键盘录入要修改的学生信息
      • 遍历修改对应的学生的信息
      • 给出修改成功的提示
    3. 调用方法

    修改学上的代码

    1. 第一种方式
    int index = -1;
            for (int i = 0; i < arrayStudent.size(); i++) {
                Student student1 = arrayStudent.get(i);
                if (student1.getsId().equals(id)) {
                    arrayStudent.set(i, student);
                    index = i;
                    break;
                }
            }
            if (index == 1) {
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败,没有此学生");
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 第二种方法
    int index = -1;
    for (Student student2 : arrayStudent) {
        if (student2.getsId().equals(id)) {
            int i = arrayStudent.indexOf(student2);
            arrayStudent.set(i,student);
            index = i;
            break;
        }
    }
    if (index == 1) {
        System.out.println("修改成功");
    }else {
        System.out.println("修改失败,没有此学生");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 全部的代码
     /**
         * 修改学生信息
     */
    public static void modifyStudent(ArrayList<Student> arrayStudent) {
            //定义键盘输入类
            Scanner input = new Scanner(System.in);
    
            System.out.println("请输入你要修改学生的学号");
            String id = input.nextLine();
            boolean flag = isUsedOrExists(arrayStudent, id);
            if (!flag) {
                System.out.println("你输入的学号不存在");
                return;
            }
            //键盘录入要修改学生信息
            System.out.println("请输入学生新姓名");
            String name = input.nextLine();
            System.out.println("请输入学生年龄");
            String age = input.nextLine();
            System.out.println("请输入新居住地");
            String address = input.nextLine();
    
            //创建学生类
            Student student = new Student();
            student.setsId(id);
            student.setName(name);
            student.setAge(age);
            student.setAddress(address);
    
            //遍历集合修改对应信息
            for (int i = 0; i < arrayStudent.size(); i++) {
                Student student1 = arrayStudent.get(i);
                if (student1.getsId().equals(id)) {
                    arrayStudent.set(i, student);
                    break;
                }
            }
    
            System.out.println("修改成功");
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    7. 解决添加学生学号重复问题

    思路:

    1. 定义一个方法,对学号是否被使用进行判断

      • 如果与集合中的某一个学生学号相同,返回true
      • 如果都不相同,返回false
    2. 在添加学生录入学号后调用给方法

      • 如果返回true,弹出提示,重新输入学号
      • 如果返回false,正常添加学生对象
     /**
         * 判断学号是否被使用
         * 或者判断学号是否存在
         */
        public static boolean isUsedOrExists(ArrayList<Student> arrayStudent,String id) {
            //学号重复返回true, 不重复,返回false
            boolean flag = false;
    
            for (Student student : arrayStudent) {
                if(student.getsId().equals(id)){
                    flag = true;
                    break;
                }
            }
            return flag;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    🎈看完了不妨给我点个赞吧,👉你的支持,是我每天更新的动力…

  • 相关阅读:
    母婴行业探秘:千万级会员体量下的精准营销
    开机explorer.exe无法正常启动,先运行Chrome后再运行explorer.exe可恢复正常,是什么原因?
    运维 | 如何在 Linux 系统中删除软链接 | Linux
    构建精致 Chrome 插件:开箱即用的 TypeScript 模板 | 开源日报 No.51
    【ChatGPT系列】ChatGPT:创新工具还是失业威胁?
    Wireshark图解三次握手
    [scikit-learn] 第一章 初识scikit-learn及内置数据集介绍
    Maven-依赖管理机制
    A-Level经济真题(8)
    性能测试 —— Tomcat监控与调优:status页监控
  • 原文地址:https://blog.csdn.net/weixin_46213083/article/details/126561630