天天看點

day12 ArrayList和學生管理系統

    • 1.ArrayList
      • 1.1ArrayList類概述【了解】
      • 1.2ArrayList類常用方法【應用】
        • 1.2.1構造方法
        • 1.2.2成員方法
        • 1.2.3示例代碼
      • 1.3ArrayList存儲字元串并周遊【應用】
        • 1.3.1案例需求
        • 1.3.2代碼實作
      • 1.4ArrayList存儲學生對象并周遊【應用】
        • 1.4.1案例需求
        • 1.4.2代碼實作
      • 1.5ArrayList存儲學生對象并周遊更新版【應用】
        • 1.5.1案例需求
        • 1.5.2代碼實作
    • 2.學生管理系統
      • 2.1學生管理系統實作步驟【了解】
      • 2.2學生類的定義【應用】
      • 2.3測試類的定義【應用】

1.ArrayList

1.1ArrayList類概述【了解】

  • 什麼是集合

    ​ 提供一種存儲空間可變的存儲模型,存儲的資料容量可以發生改變

  • ArrayList集合的特點

    ​ 底層是數組實作的,長度可以變化

  • 泛型的使用

    ​ 用于限制集合中存儲元素的資料類型

1.2ArrayList類常用方法【應用】

1.2.1構造方法

方法名 說明
public ArrayList() 建立一個空的集合對象

1.2.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) 在此集合中的指定位置插入指定的元素

1.2.3示例代碼

public class ArrayListDemo02 {
    public static void main(String[] args) {
        //建立集合
        ArrayList<String> array = new ArrayList<String>();

        //添加元素
        array.add("hello");
        array.add("world");
        array.add("java");

        //public boolean remove(Object o):删除指定的元素,傳回删除是否成功
//        System.out.println(array.remove("world"));
//        System.out.println(array.remove("javaee"));

        //public E remove(int index):删除指定索引處的元素,傳回被删除的元素
//        System.out.println(array.remove(1));

        //IndexOutOfBoundsException
//        System.out.println(array.remove(3));

        //public E set(int index,E element):修改指定索引處的元素,傳回被修改的元素
//        System.out.println(array.set(1,"javaee"));

        //IndexOutOfBoundsException
//        System.out.println(array.set(3,"javaee"));

        //public E get(int index):傳回指定索引處的元素
//        System.out.println(array.get(0));
//        System.out.println(array.get(1));
//        System.out.println(array.get(2));
        //System.out.println(array.get(3)); //?????? 自己測試

        //public int size():傳回集合中的元素的個數
        System.out.println(array.size());

        //輸出集合
        System.out.println("array:" + array);
    }
}
           

1.3ArrayList存儲字元串并周遊【應用】

1.3.1案例需求

​ 建立一個存儲字元串的集合,存儲3個字元串元素,使用程式實作在控制台周遊該集合

1.3.2代碼實作

/*
    思路:
        1:建立集合對象
        2:往集合中添加字元串對象
        3:周遊集合,首先要能夠擷取到集合中的每一個元素,這個通過get(int index)方法實作
        4:周遊集合,其次要能夠擷取到集合的長度,這個通過size()方法實作
        5:周遊集合的通用格式
 */
public class ArrayListTest01 {
    public static void main(String[] args) {
        //建立集合對象
        ArrayList<String> array = new ArrayList<String>();

        //往集合中添加字元串對象
        array.add("劉正風");
        array.add("左冷禅");
        array.add("風清揚");

        //周遊集合,其次要能夠擷取到集合的長度,這個通過size()方法實作
//        System.out.println(array.size());

        //周遊集合的通用格式
        for(int i=0; i<array.size(); i++) {
            String s = array.get(i);
            System.out.println(s);
        }
    }
}
           

1.4ArrayList存儲學生對象并周遊【應用】

1.4.1案例需求

​ 建立一個存儲學生對象的集合,存儲3個學生對象,使用程式實作在控制台周遊該集合

1.4.2代碼實作

/*
    思路:
        1:定義學生類
        2:建立集合對象
        3:建立學生對象
        4:添加學生對象到集合中
        5:周遊集合,采用通用周遊格式實作
 */
public class ArrayListTest02 {
    public static void main(String[] args) {
        //建立集合對象
        ArrayList<Student> array = new ArrayList<>();

        //建立學生對象
        Student s1 = new Student("林青霞", 30);
        Student s2 = new Student("風清揚", 33);
        Student s3 = new Student("張曼玉", 18);

        //添加學生對象到集合中
        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.5ArrayList存儲學生對象并周遊更新版【應用】

1.5.1案例需求

​ 建立一個存儲學生對象的集合,存儲3個學生對象,使用程式實作在控制台周遊該集合

​ 學生的姓名和年齡來自于鍵盤錄入

1.5.2代碼實作

/*
    思路:
        1:定義學生類,為了鍵盤錄入資料友善,把學生類中的成員變量都定義為String類型
        2:建立集合對象
        3:鍵盤錄入學生對象所需要的資料
        4:建立學生對象,把鍵盤錄入的資料指派給學生對象的成員變量
        5:往集合中添加學生對象
        6:周遊集合,采用通用周遊格式實作
 */
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());
        }
    }

    /*
        兩個明确:
            傳回值類型:void
            參數:ArrayList<Student> array
     */
    public static void addStudent(ArrayList<Student> array) {
        //鍵盤錄入學生對象所需要的資料
        Scanner sc = new Scanner(System.in);

        System.out.println("請輸入學生姓名:");
        String name = sc.nextLine();

        System.out.println("請輸入學生年齡:");
        String age = sc.nextLine();

        //建立學生對象,把鍵盤錄入的資料指派給學生對象的成員變量
        Student s = new Student();
        s.setName(name);
        s.setAge(age);

        //往集合中添加學生對象
        array.add(s);
    }
}
           

2.學生管理系統

2.1學生管理系統實作步驟【了解】

  • 案例需求

    ​ 針對目前我們的所學内容,完成一個綜合案例:學生管理系統!該系統主要功能如下:

    ​ 添加學生:通過鍵盤錄入學生資訊,添加到集合中

    ​ 删除學生:通過鍵盤錄入要删除學生的學号,将該學生對象從集合中删除

    ​ 修改學生:通過鍵盤錄入要修改學生的學号,将該學生對象其他資訊進行修改

    ​ 檢視學生:将集合中的學生對象資訊進行展示

    ​ 退出系統:結束程式

  • 實作步驟
    1. 定義學生類,包含以下成員變量

      ​ private String sid // 學生id

      ​ private String name // 學生姓名

      ​ private String age // 學生年齡

      ​ private String address // 學生所在地

    2. 學生管理系統主界面的搭建步驟

      2.1 用輸出語句完成主界面的編寫

      2.2 用Scanner實作鍵盤輸入

      2.3 用switch語句完成選擇的功能

      2.4 用循環完成功能結束後再次回到主界面

    3. 學生管理系統的添加學生功能實作步驟

      3.1 定義一個方法,接收ArrayList集合

      3.2 方法内完成添加學生的功能

      ​ ①鍵盤錄入學生資訊

      ​ ②根據錄入的資訊建立學生對象

      ​ ③将學生對象添加到集合中

      ​ ④提示添加成功資訊

      3.3 在添加學生的選項裡調用添加學生的方法

    4. 學生管理系統的檢視學生功能實作步驟

      4.1 定義一個方法,接收ArrayList集合

      4.2 方法内周遊集合,将學生資訊進行輸出

      4.3 在檢視所有學生選項裡調用檢視學生方法

    5. 學生管理系統的删除學生功能實作步驟

      5.1 定義一個方法,接收ArrayList集合

      5.2 方法中接收要删除學生的學号

      5.3 周遊集合,擷取每個學生對象

      5.4 使用學生對象的學号和錄入的要删除的學号進行比較,如果相同,則将目前學生對象從集合中删除

      5.5 在删除學生選項裡調用删除學生的方法

    6. 學生管理系統的修改學生功能實作步驟

      6.1 定義一個方法,接收ArrayList集合

      6.2 方法中接收要修改學生的學号

      6.3 通過鍵盤錄入學生對象所需的資訊,并建立對象

      6.4 周遊集合,擷取每一個學生對象。并和錄入的修改學生學号進行比較.如果相同,則使用新學生對象替換目前學生對象

      6.5 在修改學生選項裡調用修改學生的方法

    7. 退出系統

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

2.2學生類的定義【應用】

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;
    }

    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;
    }
}
           

2.3測試類的定義【應用】

public class StudentManager {
    /*
        1:用輸出語句完成主界面的編寫
        2:用Scanner實作鍵盤錄入資料
        3:用switch語句完成操作的選擇
        4:用循環完成再次回到主界面
    */
    public static void main(String[] args) {
        //建立集合對象,用于儲存學生資料資訊
        ArrayList<Student> array = new ArrayList<Student>();

        //用循環完成再次回到主界面
        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 sc = new Scanner(System.in);
            String line = sc.nextLine();

            //用switch語句完成操作的選擇
            switch (line) {
                case "1":
                    addStudent(array);
                    break;
                case "2":
                    deleteStudent(array);
                    break;
                case "3":
                    updateStudent(array);
                    break;
                case "4":
                    findAllStudent(array);
                    break;
                case "5":
                    System.out.println("謝謝使用");
                    System.exit(0); //JVM退出
            }
        }
    }

    //定義一個方法,用于添加學生資訊
    public static void addStudent(ArrayList<Student> array) {
        //鍵盤錄入學生對象所需要的資料,顯示提示資訊,提示要輸入何種資訊
        Scanner sc = new Scanner(System.in);

        String sid;

        while (true) {
            System.out.println("請輸入學生學号:");
            sid = sc.nextLine();

            boolean flag = isUsed(array, sid);
            if (flag) {
                System.out.println("你輸入的學号已經被占用,請重新輸入");
            } else {
                break;
            }
        }

        System.out.println("請輸入學生姓名:");
        String name = sc.nextLine();

        System.out.println("請輸入學生年齡:");
        String age = sc.nextLine();

        System.out.println("請輸入學生居住地:");
        String address = sc.nextLine();

        //建立學生對象,把鍵盤錄入的資料指派給學生對象的成員變量
        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        //将學生對象添加到集合中
        array.add(s);

        //給出添加成功提示
        System.out.println("添加學生成功");
    }

    //定義一個方法,判斷學号是否被使用
    public static boolean isUsed(ArrayList<Student> array, String sid) {
        //如果與集合中的某一個學生學号相同,傳回true;如果都不相同,傳回false
        boolean flag = false;

        for(int i=0; i<array.size(); i++) {
            Student s = array.get(i);
            if(s.getSid().equals(sid)) {
                flag = true;
                break;
            }
        }

        return flag;
    }


    //定義一個方法,用于檢視學生資訊
    public static void findAllStudent(ArrayList<Student> array) {
        //判斷集合中是否有資料,如果沒有顯示提示資訊
        if (array.size() == 0) {
            System.out.println("無資訊,請先添加資訊再查詢");
            //為了讓程式不再往下執行,我們在這裡寫上return;
            return;
        }

        //顯示表頭資訊
        //\t其實是一個tab鍵的位置
        System.out.println("學号\t\t\t姓名\t\t年齡\t\t居住地");

        //将集合中資料取出按照對應格式顯示學生資訊,年齡顯示補充“歲”
        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "歲\t\t" + s.getAddress());
        }
    }

    //定義一個方法,用于删除學生資訊
    public static void deleteStudent(ArrayList<Student> array) {
        //鍵盤錄入要删除的學生學号,顯示提示資訊
        Scanner sc = new Scanner(System.in);

        System.out.println("請輸入你要删除的學生的學号:");
        String sid = sc.nextLine();

        //在删除/修改學生操作前,對學号是否存在進行判斷
        //如果不存在,顯示提示資訊
        //如果存在,執行删除/修改操作

        int index = -1;

        for (int i = 0; i < array.size(); i++) {
            Student s = array.get(i);
            if (s.getSid().equals(sid)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("該資訊不存在,請重新輸入");
        } else {
            array.remove(index);
            //給出删除成功提示
            System.out.println("删除學生成功");
        }
    }

    //定義一個方法,用于修改學生資訊
    public static void updateStudent(ArrayList<Student> array) {
        //鍵盤錄入要修改的學生學号,顯示提示資訊
        Scanner sc = new Scanner(System.in);

        System.out.println("請輸入你要修改的學生的學号:");
        String sid = sc.nextLine();

        //鍵盤錄入要修改的學生資訊
        System.out.println("請輸入學生新姓名:");
        String name = sc.nextLine();
        System.out.println("請輸入學生新年齡:");
        String age = sc.nextLine();
        System.out.println("請輸入學生新居住地:");
        String address = sc.nextLine();

        //建立學生對象
        Student s = new Student();
        s.setSid(sid);
        s.setName(name);
        s.setAge(age);
        s.setAddress(address);

        //周遊集合修改對應的學生資訊
        for (int i = 0; i < array.size(); i++) {
            Student student = array.get(i);
            if (student.getSid().equals(sid)) {
                array.set(i, s);
            }
        }

        //給出修改成功提示
        System.out.println("修改學生成功");
    }
}