java基礎: ArrayList集合應用, ArrayList增刪改查詳解,綜合java基礎實現學生管理系統,


 

1.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>

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() 返回集合中的元素的個數

示例代碼 :

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.3 ArrayList存儲字符串並遍歷

案例需求 :

創建一個存儲字符串的集合,存儲3個字符串元素,使用程序實現在控制台遍歷該集合

實現步驟 :

1:創建集合對象 2:往集合中添加字符串對象 3:遍歷集合,首先要能夠獲取到集合中的每一個元素,這個通過get(int index)方法實現 4:遍歷集合,其次要能夠獲取到集合的長度,這個通過size()方法實現 5:遍歷集合的通用格式

代碼實現 :

/*
   思路:
       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.4 ArrayList存儲學生對象並遍歷

案例需求 :

創建一個存儲學生對象的集合,存儲3個學生對象,使用程序實現在控制台遍歷該集合

實現步驟 :

1:定義學生類

2:創建集合對象

3:創建學生對象

4:添加學生對象到集合中

5:遍歷集合,采用通用遍歷格式實現

代碼實現 :

/*
   思路:
       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.5 鍵盤錄入學生信息到集合

案例需求 :

創建一個存儲學生對象的集合,存儲3個學生對象,使用程序實現在控制台遍歷該集合

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

實現步驟 :

1:定義學生類,為了鍵盤錄入數據方便,把學生類中的成員變量都定義為String類型

2:創建集合對象

3:鍵盤錄入學生對象所需要的數據

4:創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量

5:往集合中添加學生對象

6:遍歷集合,采用通用遍歷格式實現

代碼實現 :

/*
   思路:
       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. 定義學生類,包含以下成員變量

      學生類: Student成員變量:

      學號:sid

      姓名:name

      年齡:age

      生日:birthday

      構造方法:

      無參構造

      帶四個參數的構造成員方法:

      每個成員變量對應給出get/set方法

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

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

      2.2 用Scanner實現鍵盤錄入數據

      2.3 用switch語句完成操作的選擇

      2.4 用循環完成再次回到主界面

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

      3.1 用鍵盤錄入選擇添加學生

      3.2 定義一個方法,用於添加學生

      顯示提示信息,提示要輸入何種信息

      鍵盤錄入學生對象所需要的數據

      創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量

      將學生對象添加到集合中(保存)

      給出添加成功提示

      3.3 調用方法

    4. 學生管理系統的查看學生功能實現步驟

      4.1 用鍵盤錄入選擇查看所有學生信息

      4.2 定義一個方法,用於查看學生信息

      顯示表頭信息

      將集合中數據取出按照對應格式顯示學生信息,年齡顯示補充“歲”

      4.3 調用方法

    5. 學生管理系統的刪除學生功能實現步驟

      5.1 用鍵盤錄入選擇刪除學生信息

      5.2 定義一個方法,用於刪除學生信息

      顯示提示信息

      鍵盤錄入要刪除的學生學號

      調用getIndex方法,查找該學號在集合的索引

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

      如果索引不是-1,調用remove方法刪除並提示刪除成功

      5.3 調用方法

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

      6.1 用鍵盤錄入選擇修改學生信息

      6.2 定義一個方法,用於修改學生信息

      顯示提示信息

      鍵盤錄入要修改的學生學號

      調用getIndex方法,查找該學號在集合的索引

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

      如果索引不是-1,鍵盤錄入要修改的學生信息

      集合修改對應的學生信息

      給出修改成功提示

      6.3 調用方法

    7. 退出系統

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

2.2 學生類的定義

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

2.3 測試類的定義

package test.TestStudent;

import 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<>();

l:
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("請輸入你的選擇:");
String choice = sc.next();
switch (choice) {
case "1":
System.out.println("添加學生");
getAddStudent(list);
break;
case "2":
//System.out.println("刪除學生");
DeleteStudent(list);
break;
case "3":

SetStudent(list);
break;
case "4":
//System.out.println("查看學生");
queryStudent(list);
break;
case "5":
System.out.println("感謝您的使用");
break l;
//return;
default:
System.out.println("你的輸入有誤!");
break;
}
}
}

//修改學生
public static void SetStudent(ArrayList<Student> list) {
System.out.println("請輸入你要修改的學號:");
Scanner sc = new Scanner(System.in);
String oldSid = sc.next();
int index = getindex(list, oldSid);
if (index == -1) {
System.out.println("查無此人!");
} else {
System.out.println("請輸入新姓名:");
String newName = sc.next();
System.out.println("請輸入新年齡:");
int newAge = sc.nextInt();
System.out.println("請輸入新生日:");
String newBirt = sc.next();
Student stu = new Student(oldSid, newName, newAge, newBirt);
list.set(index, stu);
System.out.println("修改成功!");
}
}
//刪除學生
public static void DeleteStudent(ArrayList<Student> list) {
//1.給出提示信息(請輸入你要刪除的學號)
System.out.println("請輸入你要刪除的學生學號:");
Scanner sc = new Scanner(System.in);
//2.鍵盤接收要刪除的學號
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 int getindex(ArrayList<Student> list, String sid) {
//1.假設傳入的學號在數據中不存在

//2.遍歷集合,獲取每一個學生對象,准備進行查找
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
String sid1 = stu.getSid();
if (sid.equals(sid1) == true) {
return i;
}
}
return -1;
//3.獲取每一個學生對象的學號
}
//查看學生方法
public static void queryStudent(ArrayList<Student> list) {
//1.判斷集合中是否存在數據,如果不存在給出信息
//2.存在:展示表頭數據
//3.遍歷集合,獲取每一個學生對象信息打印在控制台
if (list.size() == 0) {
System.out.println("無信息,請添加后重新查詢!");
return;
}
System.out.println("學號\t\t姓名\t\t年齡\t\t生日");
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + " \t\t" +
stu.getBirt());
}

}
//添加學生方法
public static ArrayList<Student> getAddStudent(ArrayList<Student> list) {
//1.給出錄入提示;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("請輸入學號:");
String sid = sc.next();
int index = getindex(list, sid);
if (index == -1) {
System.out.println("請輸入姓名:");
String name = sc.next();
System.out.println("請輸入年齡:");
int age = sc.nextInt();
System.out.println("請輸入生日:");
String birthdy = sc.next();
//2.將鍵盤錄入數據封裝為學生對象;
Student stu = new Student(sid, name, age, birthdy);
//3.將封裝對象添加到集合;
list.add(stu);
//4.給出添加成功提示;
System.out.println("添加成功!");
break;
} else {
System.out.println("學號已被占用,請重新輸入!");
}

}
return list;

}
}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM