• 14.集合


    1.ArrayList

    对象数组

    需求:将(张三,23)(李四,24)(王五,25)封装为3个学生对象并存入数组

    随后遍历数组,将学生信息输出在控制台

    1.定义学生类准备用于封装数据

    2.动态初始化长度为3的数组,类型为Student类型

    3.根据需求创建3个学生对象

    4.将学生对象存入数组

    5.遍历数组,取出每一个学生对象

    6.调用对象的getXxx方法获取学生信息,并输出在控制台

    代码:

    Student类

    package com.heima.domain;
    
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    package com.heima.array;
    
    import com.heima.domain.Student;
    
    public class TestObjectArray {
        /*
           需求:将(张三,23)(李四,24)(王五,25)
               封装为3个学生对象并存入数组
               随后遍历数组,将学生信息输出在控制台
    
           思路:
               1. 定义学生类准备用于封装数据
               2. 动态初始化长度为3的数组,类型为Student类型
               3. 根据需求创建3个学生对象
               4. 将学生对象存入数组
               5. 遍历数组,取出每一个学生对象
               6. 调用对象的getXxx方法获取学生信息,并输出在控制台
        */
        public static void main(String[] args) {
            // 2. 动态初始化长度为3的数组,类型为Student类型
            Student[] arr = new Student[3];
            // 3. 根据需求创建3个学生对象
            Student stu1 = new Student("张三",23);
            Student stu2 = new Student("李四",24);
            Student stu3 = new Student("王五",25);
            // 4. 将学生对象存入数组
            arr[0] = stu1;
            arr[1] = stu2;
            arr[2] = stu3;
            // 5. 遍历数组,取出每一个学生对象
            for (int i = 0; i < arr.length; i++) {
                Student temp = arr[i];
                System.out.println(temp.getName() + "..." + temp.getAge());
            }
        }
    
    }
    

    集合和数组的特点对比

    集合类的特点:提供一种存储空间可变的存储模型,存储的数据容量可以发生改变  

    集合和数组的区别 :

        共同点:都是存储数据的容器

        不同点:数组的容量是固定的,集合的容量是可变的

    如果存储的数据,长度经常发生改变,推荐使用集合

    1.ArrayList集合

    集合类有很多,目前我们先学习一个:ArrayList

    1.1 -ArrayList的构造方法和添加方法

    public ArrayList()创建一个空的集合对象
    public boolean add(E e) 将指定的元素追加到此集合的末尾
    public void add(int index,E element) 在此集合中的指定位置插入指定的元素

    ArrayList<E>

    可调整大小的数组实现

    <E> : 是一种特殊的数据类型,泛型

    怎么用呢 ?

    在出现E的地方我们使用引用数据类型替换即可

    举例:ArrayList<String>, ArrayList<Student>

    package com.heima.list;
    
    import java.util.ArrayList;
    
    public class Demo1ArrayList {
        /*
          ArrayList构造方法:
                  ArrayList() 构造一个初始容量为 10 的空列表。
    
          成员方法:
              添加:
                  boolean add(E e) 将指定的元素添加到此列表的尾部。
                  void add(int index, E element) 将指定的元素插入此列表中的指定位置。
    
    
          注意:
                  集合容器如果没有加入<> 就可以存储任意数据类型
    
                  <> 泛型: 对集合容器存储的数据类型进行限制
       */
        public static void main(String[] args) {
            // 1. 创建集合容器对象
            ArrayList<String> list = new ArrayList<>();
            // 2. 调用对象的add方法, 向容器中添加数据
            list.add("111");
            list.add("222");
            list.add("333");
            list.add("444");
            list.add("555");
    
            list.add(0,"666");
    
    
            System.out.println(list);
        }
    }

    1.2ArrayList类常用方法【应用】

    成员方法 :

    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() 返回集合中的元素的个数
    package com.heima.list;
    
    import java.util.ArrayList;
    
    public class Demo2ArrayList {
        /*
           集合常用成员方法:
    
               添加:
                   boolean add(E e) 将指定的元素添加到此列表的尾部。
                   void add(int index, E element) 将指定的元素插入此列表中的指定位置。
    
               删除:
                   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 static void main(String[] args) {
            ArrayList<String> list = new ArrayList<>();
            list.add("abc");
            list.add("111");
            list.add("222");
            list.add("333");
            list.add("444");
            list.add("555");
    
            // public E get(int index) 返回指定索引处的元素
            String s1 = list.get(0);
            String s2 = list.get(1);
            String s3 = list.get(2);
    
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
    
            // public int size() 返回集合中的元素的个数
            int size = list.size();
            System.out.println(size);
    
        }
    
        public static void testSet() {
            ArrayList<String> list = new ArrayList<>();
            list.add("abc");
            list.add("111");
            list.add("222");
            list.add("333");
            list.add("444");
            list.add("555");
    
            //  public E set(int index,E element) 修改指定索引处的元素,返回被修改的元素
            String s = list.set(0,"666");
            System.out.println(s);
    
            System.out.println(list);
        }
    
        public static void testRemove() {
            ArrayList<String> list = new ArrayList<>();
            list.add("abc");
            list.add("111");
            list.add("222");
            list.add("333");
            list.add("444");
            list.add("555");
            //  public boolean remove(Object o) 删除指定的元素,返回删除是否成功
            boolean b1 = list.remove("abc");
            boolean b2 = list.remove("zzz");
    
            System.out.println(b1);
            System.out.println(b2);
    
            System.out.println(list);
    
            // public E remove(int index)  删除指定索引处的元素,返回被删除的元素
            String s = list.remove(0);
            System.out.println(s);
    
            System.out.println(list);
        }
    }
    

    1.3 ArrayList存储字符串并遍历

    案例需求 :

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

    实现步骤 :

    1:创建集合对象

    2:往集合中添加字符串对象

    3:遍历集合,首先要能够获取到集合中的每一个元素,这个通过get(int index)方法实现

    4:遍历集合,其次要能够获取到集合的长度,这个通过size()方法实现

    5:遍历集合的通用格式

    for(int i=0; i<集合对象.size(); i++) {
        集合对象.get(i)就是指定索引处的元素

    }

    代码实现 :

    package com.heima.test;
    
    import java.util.ArrayList;
    
    public class Test1 {
        /*
          需求:创建一个存储字符串的集合,存储3个字符串元素,使用程序实现在控制台遍历该集合
    
          思路:
              1. 创建集合对象
              2. 往集合中添加字符串对象
              3. 遍历集合
                    获取到集合中的每一个元素,这个通过get(int index)方法实现
                    获取到集合的长度,这个通过size()方法实现
    
       */
        public static void main(String[] args) {
            // 1. 创建集合对象
            ArrayList<String> list = new ArrayList<>();
            // 2. 往集合中添加字符串对象
            list.add("张三");
            list.add("李四");
            list.add("王五");
            // 3. 遍历集合
            for (int i = 0; i < list.size(); i++) {
            //i:每一个索引 String s = list.get(i); System.out.println(s); } } }

    1.4 ArrayList存储学生对象并遍历

    案例需求 :

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

    实现步骤 :

    1:定义学生类

    2:创建集合对象

    3:创建学生对象

    4:添加学生对象到集合中

    5:遍历集合,采用通用遍历格式实现

    代码实现 :

    Student类

    package com.heima.domain;
    
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    Test2类

    package com.heima.test;
    
    import com.heima.domain.Student;
    
    import java.util.ArrayList;
    
    public class Test2 {
        /*
            需求:创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
    
            思路:
                1. 定义学生类
                2. 创建集合对象
                3. 创建学生对象
                4. 添加学生对象到集合中
                5. 遍历集合,采用通用遍历格式实现
         */
        public static void main(String[] args) {
            // 2. 创建集合对象
            ArrayList<Student> list = new ArrayList<>();
            // 3. 创建学生对象
            Student stu1 = new Student("张三1",23);
            Student stu2 = new Student("张三2",23);
            Student stu3 = new Student("张三3",23);
            // 4. 添加学生对象到集合中
            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
            // 5. 遍历集合,采用通用遍历格式实现
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getName() + "..." + stu.getAge());
            }
        }
    }
    

    1.5 键盘录入学生信息到集合

    案例需求 :

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

    学生的姓名和年龄来自于键盘录入

    实现步骤 :

    1:定义学生类,为了键盘录入数据方便,把学生类中的成员变量都定义为String类型

    2:创建集合对象

    3:键盘录入学生对象所需要的数据

    4:创建学生对象,把键盘录入的数据赋值给学生对象的成员变量

    5:往集合中添加学生对象

    6:遍历集合,采用通用遍历格式实现

    代码实现 :

    Student类

    package com.heima.domain;
    
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

      

    Test3类

    package com.heima.test;
    
    import com.heima.domain.Student;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Test3 {
        /*
            需求:创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
              学生的姓名和年龄来自于键盘录入
    
            思路:
                1. 定义学生类
                2. 创建集合对象
                3. 键盘录入学生对象所需要的数据
                4. 创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
                5. 往集合中添加学生对象
                6. 遍历集合,采用通用遍历格式实现
    
         */
        public static void main(String[] args) {
            // 2. 创建集合对象
            ArrayList<Student> list = new ArrayList<>();
            Student stu1 = getStudent();
            Student stu2 = getStudent();
            Student stu3 = getStudent();
            // 5. 往集合中添加学生对象
            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
            //  6. 遍历集合,采用通用遍历格式实现
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getName() + "..." + stu.getAge());
            }
        }
    
        public static Student getStudent() {
            // 3. 键盘录入学生对象所需要的数据
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入学生姓名:");
            // next()   nextInt();
            String name = sc.next();
            System.out.println("请输入学生年龄:");
            int age = sc.nextInt();
            // 4. 创建学生对象,把键盘录入的数据赋值给学生对象的成员变量
            Student stu = new Student(name, age);
    
            return stu;
        }
    }
    

      集合删除元素

    需求:创建一个存储String的集合,内部存储(test,张三,李四,test,test)字符串

    删除所有的test字符串,删除后,将集合元素打印在控制台

    思路:

    1.创建集合对象

    2.调用add方法,添加字符串

    3.遍历集合,取出每一个字符串元素

    4.加入if判断,如果是test字符串,调用remove方法删除  

    Student类

    package com.heima.domain;
    
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

      

    Test4类

    package com.heima.test;
    
    import java.util.ArrayList;
    
    public class Test4 {
        /*
            需求:创建一个存储String的集合
            内部存储(test,张三,李四,test,test)字符串
            删除所有的test字符串,删除后,将集合剩余元素打印在控制台
    
            思路:
            1. 创建集合对象
            2. 调用add方法,添加字符串
            3. 遍历集合,取出每一个字符串元素
            4. 加入if判断,如果是test字符串,调用remove方法删除
            5. 打印集合元素
    
           // 注意: 根据元素进行删除, 只能删除集合中的第一个元素
            list.remove("test");
         */
        public static void main(String[] args) {
            // 1. 创建集合对象
            ArrayList<String> list = new ArrayList<>();
            // 2. 调用add方法,添加字符串
            list.add("test");
            list.add("张三");
            list.add("李四");
            list.add("test");
            list.add("test");
            // 3. 遍历集合,取出每一个字符串元素
            for (int i = 0; i < list.size(); i++) {
                String s = list.get(i);
                // 4. 加入if判断,如果是test字符串,调用remove方法删除
                //if(s.equals("test")){}
                //用常量调用方法
                if("test".equals(s)){
                    list.remove(i);
                    i--;
                }
            }
            System.out.println(list);
        }
    }
    

      集合元素筛选

    需求:定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出来并存入新集合对象,方法返回新集合。

    思路:

    1:定义方法,方法的形象定义为ArrayList<Student> list

    2.方法内部定义新集合,准备存储筛选出的学生对象ArrayList<Student>newList 

    3.遍历原集合,获取每一个学生对象 

    4.通过学生对象调用getAge方法获取年龄,并判断年龄是否低于18

    5.将年龄低于18的学生对象存入新集合

    6返回新集合 

    7.main方法测试该方法

    代码示例:

    Student类

    package com.heima.domain;
    
    public class Student {
        private String name;
        private int age;
    
        public Student() {
        }
    
        public Student(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    

    Test5

    package com.heima.test;
    
    import com.heima.domain.Student;
    
    import java.util.ArrayList;
    
    public class Test5 {
        /*
            需求:定义一个方法,方法接收一个集合对象(泛型为Student)
                方法内部将年龄低于18的学生对象找出
                并存入新集合对象,方法返回新集合。
    
            思路:
                1. 定义方法,方法的形参定义为ArrayList<Student> list
                2. 方法内部定义新集合,准备存储筛选出的学生对象 ArrayList<Student> newList
                3. 遍历原集合,获取每一个学生对象
                4. 通过学生对象调用getAge方法获取年龄,并判断年龄是否低于18
                5. 将年龄低于18的学生对象存入新集合
                6. 返回新集合
                7. main方法中测试该方法
         */
        public static void main(String[] args) {
            // 7. main方法中测试该方法
            ArrayList<Student> list = new ArrayList<>();
            Student stu1 = new Student("张三1",10);
            Student stu2 = new Student("张三2",10);
            Student stu3 = new Student("张三3",20);
            list.add(stu1);
            list.add(stu2);
            list.add(stu3);
    
            ArrayList<Student> newList = getList(list);
            for (int i = 0; i < newList.size(); i++) {
                Student stu = newList.get(i);
                System.out.println(stu.getName() + "..." + stu.getAge());
            }
    
        }
    
        // 1. 定义方法,方法的形参定义为ArrayList<Student> list
        public static ArrayList<Student> getList(ArrayList<Student> list) {
            // 2. 方法内部定义新集合,准备存储筛选出的学生对象 ArrayList<Student> newList
            ArrayList<Student> newList = new ArrayList<>();
            // 3. 遍历原集合,获取每一个学生对象
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                // 4. 通过学生对象调用getAge方法获取年龄,并判断年龄是否低于18
                int age = stu.getAge();
                if (age < 18) {
                    //  5. 将年龄低于18的学生对象存入新集合
                    newList.add(stu);
                }
            }
            // 6. 返回新集合
            return newList;
        }
    }
    

    2. 学生管理系统

    2.1 学生管理系统实现步骤

    案例需求

    针对目前我们的所学内容,完成一个综合案例:学生管理系统!该系统主要功能如下:

    添加学生:通过键盘录入学生信息,添加到集合中

    删除学生:通过键盘录入要删除学生的学号,将该学生对象从集合中删除

    修改学生:通过键盘录入要修改学生的学号,将该学生对象其他信息进行修改

    查看学生:将集合中的学生对象信息进行展示

    退出系统:结束程序

    实现步骤

     

    定义学生类,包含以下成员变量

    学生类: Student成员变量:

    学号:sid

    姓名:name

    年龄:age

    生日:birthday

    构造方法:

      无参构造

      带四个参数的构造成员方法:

    每个成员变量对应给出get/set方法

    Student类:

    package com.itheima.domain;
    
    public class Student {
        private String sid;//学号
        private String name;//姓名
        private int age;//年龄
        private String birthday;//生日
    
        public Student() {
        }
    
        public Student(String sid, String name, int age, String birthday) {
            this.sid = sid;
            this.name = name;
            this.age = age;
            this.birthday = birthday;
        }
    
        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 int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        public String getBirthday() {
            return birthday;
        }
    
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
    } 

    学生管理系统主界面的搭建步骤

    1 用输出语句完成主界面的编写

    2 用Scanner实现键盘录入数据

    3 用switch语句完成操作的选择

    4 用循环完成再次回到主界面

    学生管理系统的添加学生功能实现步骤

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

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

      显示提示信息,提示要输入何种信息

      键盘录入学生对象所需要的数据

      创建学生对象,把键盘录入的数据赋值给学生对象的成员变量

      将学生对象添加到集合中(保存)

      给出添加成功提示

    3. 调用方法

    学生管理系统的查看学生功能实现步骤

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

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

      判定集合中是否有数据,如果没有显示提示信息

      显示表头信息

      将集合中数据取出按照对应格式显示学生信息,年龄显示补充“岁”

    3 调用方法

    ArrayList无数据:无信息,请先添加信息再查询!

     

    删除/修改学生学号不存在问题

    在删除/修改学生操作前,对学号是否存在进行判断

    思路:定义一个方法,该方法用于从集合中,查找【学号】【集合】中出现的索引位置

    学号存在:返回正确的索引位置-----------------------集合调用remove方法,根据索引删除

    学号不存在:返回-1-----------------------控制台显示,信息不存在

    学生管理系统的删除学生功能实现步骤

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

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

      显示提示信息

      键盘录入要删除的学生学号

      调用getIndex方法,查找该学号在集合的索引

      如果索引为-1,提示信息不存在

      如果索引不是-1,调用remove方法删除并提示删除成功

    3 调用方法

    学生管理系统的修改学生功能实现步骤

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

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

      显示提示信息

      键盘录入要修改的学生学号

      调用getIndex方法,查找该学号在集合的索引

      如果索引为-1,提示信息不存在

      如果索引不是-1,键盘录入要修改的学生信息

      集合修改对应的学生信息

      给出修改成功提示

    3 调用方法

    退出系统

    使用System.exit(0);退出JVM

    ArrayList<Student> 集合容器

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

    思路:

    1.在添加学生录入学号后,调用getIndex方法

    2.根据方法的返回值,判断学号是否存在

      返回值为-1:不存在,可以存储

      返回值为正确索引,存在,给出提示,重新接收信息

    测试类:

    package com.itheima.test;
    
    import com.itheima.domain.Student;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class StudentManager {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
    
            // 创建集合容器对象
            ArrayList<Student> list = new ArrayList<>();
    
            lo:
            while (true) {
                // 1. 搭建主界面菜单
                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("请输入您的选择:");
    
                String choice = sc.next();
    
                switch (choice) {
                    case "1":
                        //System.out.println("添加学生");
                        addStudent(list);
                        break;
                    case "2":
                        //System.out.println("删除学生");
                        deleteStudent(list);
                        break;
                    case "3":
                        //System.out.println("修改学生");
                        updateStudent(list);
                        break;
                    case "4":
                        // System.out.println("查看学生");
                        queryStudents(list);
                        break;
                    case "5":
                        System.out.println("感谢您的使用");
                        break lo;
                    default:
                        System.out.println("您的输入有误");
                        break;
                }
            }
    
    
        }
    
        // 修改学生的方法
        public static void updateStudent(ArrayList<Student> list) {
            System.out.println("请输入您要修改的学生学号:");
            Scanner sc = new Scanner(System.in);
            String updateSid = sc.next();
            // 3. 调用getIndex方法, 查找该学号在集合中出现的索引位置
            int index = getIndex(list,updateSid);
            // 4. 根据索引判断, 学号在集合中是否存在
            if(index == -1){
                // 不存在: 给出提示
                System.out.println("查无信息, 请重新输入");
            }else{
                // 存在: 接收新的学生信息
                System.out.println("请输入新的学生姓名:");
                String name = sc.next();
                System.out.println("请输入新的学生年龄:");
                int age = sc.nextInt();
                System.out.println("请输入新的学生生日:");
                String birthday = sc.next();
                // 封装为新的学生对象
                Student stu = new Student(updateSid, name, age, birthday);
                // 调用集合的set方法, 完成修改
                list.set(index, stu);
                System.out.println("修改成功!");
            }
        }
    
        // 删除学生的方法
        public static void deleteStudent(ArrayList<Student> list) {
            // 1. 给出提示信息 (请输入您要删除的学号)
            System.out.println("请输入您要删除的学生学号:");
            // 2. 键盘接收要删除的学号
            Scanner sc = new Scanner(System.in);
            String deleteSid = sc.next();
            // 3. 调用getIndex方法, 查找该学号在集合中出现的索引位置
            int index = getIndex(list,deleteSid);
            // 4. 根据索引判断, 学号在集合中是否存在
            if(index == -1){
                // 不存在: 给出提示
                System.out.println("查无信息, 请重新输入");
            }else{
                // 存在:删除
                list.remove(index);
                System.out.println("删除成功!");
            }
        }
    
        // 查看学生的方法
        public static void queryStudents(ArrayList<Student> list) {
            // 1. 判断集合中是否存在数据, 如果不存在直接给出提示
            if(list.size() == 0){
                System.out.println("无信息, 请添加后重新查询");
                return;
            }
            // 2. 存在: 展示表头数据
            System.out.println("学号		姓名	年龄	生日");
            // 3. 遍历集合, 获取每一个学生对象的信息, 打印在控制台
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                System.out.println(stu.getSid() + "	" + stu.getName() + "	" + stu.getAge() + "		" + stu.getBirthday());
            }
        }
    
        // 添加学生的方法
        public static void addStudent(ArrayList<Student> list) {
            Scanner sc = new Scanner(System.in);
            // 1. 给出录入的提示信息
    
            String sid;
    
            while(true){
                System.out.println("请输入学号:");
                sid = sc.next();
    
                int index = getIndex(list, sid);
    
                if(index == -1){
                    // sid不存在, 学号可以使用
                    break;
                }
            }
    
            System.out.println("请输入姓名:");
            String name = sc.next();
            System.out.println("请输入年龄:");
            int age = sc.nextInt();
            System.out.println("请输入生日:");
            String birthday = sc.next();
            // 2. 将键盘录入的信息封装为学生对象
            Student stu = new Student(sid,name,age,birthday);
            // 3. 将封装好的学生对象, 添加到集合容器当中
            list.add(stu);
            // 4. 给出添加成功的提示信息
            System.out.println("添加成功!");
        }
    
        /*
            getIndex : 接收一个集合对象, 接收一个学生学号
    
            查找这个学号, 在集合中出现的索引位置
         */
        public static int getIndex(ArrayList<Student> list, String sid){
            // 1. 假设传入的学号, 在集合中不存在
            int index = -1;
            // 2. 遍历集合, 获取每一个学生对象, 准备进行查找
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                // 3. 获取每一个学生对象的学号
                String id = stu.getSid();
                // 4. 使用获取出的学生学号, 和传入的学号(查找的学号)进行比对
                if(id.equals(sid)){
                    // 存在: 让index变量记录正确的索引位置
                    index = i;
                }
            }
    
            return index;
        }
    }
    

      

      

      

     

     

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    设计模式-总览
    restful
    springmvc异常统一处理(二)
    springmvc参数校验(二)
    跨语言的RPC
    更安全的RPC接口
    RPC版 “Hello World”
    用例(Use Case)
    参与者
    数据库知识基础入门
  • 原文地址:https://www.cnblogs.com/faded8679/p/13874699.html
Copyright © 2020-2023  润新知