今日內容介紹
- 對象數組
- 集合類之ArrayList
- 學生管理系統案例
- 對象數組
- 對象數組概述
A:基本類型的數組:存儲的元素爲基本類型
int[] arr={1,2,3,4}
B:對象數組:存儲的元素爲引用類型
Student[] stus=new Student[3];
Student代表一個自定義類
Stus數組中stus[0],stus[1],stus[2]的元素數據類型爲Student,
都可以指向一個Student對象
-
- 對象數組案例:
創建一個學生數組,存儲三個學生對象並遍歷
-
-
- 案例代碼一:
-
package com.it;
/*
* 自動生成構造方法:
* 代碼區域右鍵 -- Source -- Generate Constructors from Superclass... 無參構造方法
* 代碼區域右鍵 -- Source -- Generate Constructor using Fields... 帶參構造方法
* 自動生成getXxx()/setXxx():
* 代碼區域右鍵 -- Source -- Generate Getters and Setters...
*/
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.itheima;
/*
* 創建一個學生數組,存儲三個學生對象並遍歷
*
* 分析:
* A:定義學生類
* B:創建學生數組
* C:創建學生對象
* D:把學生對象作爲元素賦值給學生數組
* E:遍歷學生數組
*/
public class StudentDemo {
public static void main(String[] args) {
//創建學生數組
Student[] students = new Student[3];
//創建學生對象
Student s1 = new Student("曹操",40);
Student s2 = new Student("劉備",35);
Student s3 = new Student("孫權",30);
//把學生對象作爲元素賦值給學生數組
students[0] = s1;
students[1] = s2;
students[2] = s3;
//遍歷學生數組
for(int x=0; x<students.length; x++) {
Student s = students[x];
//System.out.println(s);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
-
- 對象數組的內存圖
- 集合類之ArrayList
- 集合概述
A:我們學習的是面向對象編程語言,而面向對象編程語言對事物的描述都是通過對象來體現的。
爲了方便對多個對象進行操作,我們就必須對這多個對象進行存儲,而要想對多個對象進行存儲, 就不能是一個基本的變量,而應該是一個容器類型的變量。
B:到目前爲止,我們學習過了哪些容器類型的數據呢?
StringBuilder,數組。
StringBuilder的結果只能是一個字符串類型,不一定滿足我們的需求。
所以,我們目前只能選擇數組了,也就是我們前面學習過的對象數組。
但是,數組的長度是固定的, 如果有時候元素的個數不確定的,我們無法定義出數組的長度,這個時候,java就提供了集合類供我們使用。
-
- ArrayList集合
- ArrayList添加新元素
- 案例代碼二:
- ArrayList添加新元素
- ArrayList集合
package com.it_01;
import java.util.ArrayList;
/*
* 爲什麼會出現集合類:
* 我們學習的是面向對象編程語言,而面向對象編程語言對事物的描述都是通過對象來體現的。
* 爲了方便對多個對象進行操作,我們就必須對這多個對象進行存儲,而要想對多個對象進行存儲,
* 就不能是一個基本的變量,而應該是一個容器類型的變量。
* 到目前爲止,我們學習過了哪些容器類型的數據呢?StringBuilder,數組。
* StringBuilder的結果只能是一個字符串類型,不一定滿足我們的需求。
* 所以,我們目前只能選擇數組了,也就是我們前面學習過的對象數組。
* 但是,數組的長度是固定的,適應不了變化的需求,那麼,我們該如何選擇呢?
* 這個時候,java就提供了集合類供我們使用。
*
* 集合類的特點:
* 長度可變。
*
* ArrayList<E>:
* 大小可變數組的實現
*
* <E>:是一種特殊的數據類型,泛型。
* 怎麼用呢?
* 在出現E的地方我們使用引用數據類型替換即可
* 舉例:ArrayList<String>,ArrayList<Student>
*
* 構造方法:
* ArrayList()
*
* 添加元素:
* public boolean add(E e):添加元素
* public void add(int index,E element):在指定的索引處添加一個元素
*/
public class ArrayListDemo {
public static void main(String[] args) {
//創建集合對象
ArrayList<String> array = new ArrayList<String>();
//add(E e):添加元素
array.add("hello");
array.add("world");
array.add("java");
//add(int index,E element):在指定的索引處添加一個元素
//array.add(1, "android");
System.out.println("array:"+array);
}
}
ArrayList刪改查方法
A:獲取元素
public E get(int index):返回指定索引處的元素
B:集合長度
public int size():返回集合中的元素的個數
C:刪除元素
public boolean remove(Object o):刪除指定的元素,返回刪除是否成功
public E remove(int index):刪除指定索引處的元素,返回被刪除的元素
D:修改元素
public E set(int index,E element):修改指定索引處的元素,返回被修改的元素
案例代碼三:
package com.itheima_01;
import java.util.ArrayList;
/*
* 獲取元素
* public E get(int index):返回指定索引處的元素
* 集合長度
* public int size():返回集合中的元素的個數
* 刪除元素
* public boolean remove(Object o):刪除指定的元素,返回刪除是否成功
* public E remove(int index):刪除指定索引處的元素,返回被刪除的元素
* 修改元素
* public E set(int index,E element):修改指定索引處的元素,返回被修改的元素
*/
public class ArrayListDemo2 {
public static void main(String[] args) {
//創建集合對象
ArrayList<String> array = new ArrayList<String>();
//添加元素
array.add("hello");
array.add("world");
array.add("java");
//public E get(int index):返回指定索引處的元素
//System.out.println("get:"+array.get(0));
//System.out.println("get:"+array.get(1));
//System.out.println("get:"+array.get(2));
//public int size():返回集合中的元素的個數
//System.out.println("size:"+array.size());
//public boolean remove(Object o):刪除指定的元素,返回刪除是否成功
//System.out.println("remove:"+array.remove("world"));//true
//System.out.println("remove:"+array.remove("world"));//false
//public E remove(int index):刪除指定索引處的元素,返回被刪除的元素
//System.out.println("remove:"+array.remove(0));
//public E set(int index,E element):修改指定索引處的元素,返回被修改的元素
System.out.println("set:"+array.set(1, "android"));
//輸出
System.out.println("array:"+array);
}
}
-
-
- ArrayList遍歷
-
集合的遍歷思想和數組的遍歷思想相同
循環遍歷容器,依次取出裏面的元素即可
-
-
-
- 案例代碼四:
-
-
package com.it_01;
import java.util.ArrayList;
/*
* ArrayList集合的遍歷
* 通過size()和get()配合實現的
*/
public class ArrayListDemo3 {
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.get(0));
System.out.println(array.get(1));
System.out.println(array.get(2));
System.out.println("----------");
for(int x=0; x<3; x++) {
System.out.println(array.get(x));
}
System.out.println("----------");
//如何知道集合中元素的個數呢?size()
for(int x=0; x<array.size(); x++) {
System.out.println(array.get(x));
}
System.out.println("----------");
//最標準的用法
for(int x=0; x<array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
}
}
-
- ArrayList集合案例
- ArrayList練習之存儲字符串並遍歷
- ArrayList集合案例
向集合中添加任意四個字符串,遍歷集合,依次打印取出的字符串
-
-
-
- 案例代碼五:
-
-
package com.it_02;
import java.util.ArrayList;
/*
* 存儲字符串並遍歷
*
* 分析:
* A:創建集合對象
* B:添加字符串元素
* C:遍歷集合
*/
public class ArrayListTest {
public static void main(String[] args) {
//創建集合對象
ArrayList<String> array = new ArrayList<String>();
//添加字符串元素
array.add("向問天");
array.add("劉正風");
array.add("左冷禪");
array.add("風清揚");
//遍歷集合
for(int x=0; x<array.size(); x++) {
String s = array.get(x);
System.out.println(s);
}
}
}
-
-
- ArrayList練習之獲取滿足要求的元素
-
給定一個字符串數組:{“張三丰”,“宋遠橋”,“張無忌”,“殷梨亭”“張翠山”,“莫聲谷”},將數組中的元素添加到集合中,並把所有姓張的人員打印到控制檯上
-
-
-
- 案例代碼六:
-
-
package com.it_02;
import java.util.ArrayList;
/*
* 給定一個字符串數組:{“張三丰”,“宋遠橋”,“張無忌”,“殷梨亭”,“張翠山”,“莫聲谷”},將數組中的元素添加到集合中,並把所有姓張的人員打印到控制檯上。
*
* 分析:
* A:定義字符串數組
* B:創建集合對象
* C:遍歷字符串數組,獲取到每一個字符串元素
* D:把獲取到的字符串元素添加到集合
* E:遍歷集合
* 要判斷每一個字符串元素是否以"張"開頭,如果是,就輸出在控制檯
*/
public class ArrayListTest2 {
public static void main(String[] args) {
//定義字符串數組
String[] strArray = {"張三丰","宋遠橋","張無忌","殷梨亭","張翠山","莫聲谷"};
//創建集合對象
ArrayList<String> array = new ArrayList<String>();
//遍歷字符串數組,獲取到每一個字符串元素
for(int x=0; x<strArray.length; x++) {
//把獲取到的字符串元素添加到集合
array.add(strArray[x]);
}
//遍歷集合
for(int x=0; x<array.size(); x++) {
String s = array.get(x);
//要判斷每一個字符串元素是否以"張"開頭,如果是,就輸出在控制檯
if(s.startsWith("張")) {
System.out.println(s);
}
}
}
}
-
-
- ArrayList練習之存儲自定義對象並遍歷
-
A:自定義一個學生類,學生中有姓名和年齡屬性,生成滿參構造與空參構造
生成屬性對應的getter/setter方法
B:在測試類中使用滿參構造創建三個學生對象,然後將每個學生對象均添加到ArrayList集合中
C:遍歷這個ArrayList集合,依次打印出每個學生的姓名和年齡
-
-
-
- 案例代碼七:
-
-
package com.it_02;
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.itheima_02;
import java.util.ArrayList;
/*
* 存儲自定義對象並遍歷
*
* 分析:
* A:定義學生類
* B:創建集合對象
* C:創建學生對象
* D:把學生對象作爲元素添加到集合中
* E:遍歷集合
*/
public class ArrayListTest3 {
public static void main(String[] args) {
//創建集合對象
ArrayList<Student> array = new ArrayList<Student>();
//創建學生對象
Student s1 = new Student("林青霞",28);
Student s2 = new Student("張曼玉",30);
Student s3 = new Student("景甜",25);
Student s4 = new Student("柳巖",18);
//把學生對象作爲元素添加到集合中
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
//遍歷集合
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
-
-
- ArrayList練習之鍵盤錄入數據存儲並遍歷
-
創建一個Student類包含姓名和年齡屬性
創建一個ArrayList集合
向集合中添加三個Student對象Student對象中姓名和年齡的數據均來自與鍵盤錄入
最終遍歷這個集合,取出Student對象以及裏面屬性的值
-
-
-
- 案例代碼八:
-
-
package com.it_03;
public class Student {
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
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;
}
}
package com.itheima_03;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 創建一個集合,存儲學生對象,學生對象的數據來自於鍵盤錄入,最後,遍歷集合
*
* 注意:爲了方便使用,我把學生類中的所有成員定義爲String類型
*
* 分析:
* A:定義學生類
* B:創建集合對象
* C:鍵盤錄入數據,創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量
* D:把學生對象作爲元素存儲到集合中
* E:遍歷集合
*
*/
public class StudentDemo {
public static void main(String[] args) {
//創建集合對象
ArrayList<Student> array = new ArrayList<Student>();
/*
//鍵盤錄入數據,創建學生對象,把鍵盤錄入的數據賦值給學生對象的成員變量
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);
*/
//爲了提高代碼的複用性,我把鍵盤錄入數據給學生對象,並存儲到集合中的動作用一個方法來實現
//調用方法
addStudent(array);
addStudent(array);
addStudent(array);
//遍歷集合
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
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);
}
}
- 學生管理系統案例
- 學生管理系統案例需求
利用集合完成對學生的增刪改查四個功能
-
- 學生管理系統案例實現
- 創建學生類:
- 案例代碼九:
- 創建學生類:
- 學生管理系統案例實現
package com.it;
/*
* 這是我的學生類
*/
public class Student {
//學號
private String id;
//姓名
private String name;
//年齡
private String age;
//居住地
private String address;
public Student() {
}
public Student(String id, String name, String age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
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;
}
}
-
-
- 學生管理系統界面實現:
- 案例代碼十:
- 學生管理系統界面實現:
-
package com.it;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 這是我的學生管理系統的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統的主界面的代碼編寫
* C:學生管理系統的查看所有學生的代碼編寫
* D:學生管理系統的添加學生的代碼編寫
* E:學生管理系統的刪除學生的代碼編寫
* F:學生管理系統的修改學生的代碼編寫
*/
public class StudentManagerTest {
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 sc = new Scanner(System.in);
String choiceString = sc.nextLine();
// 用switch語句實現選擇
switch (choiceString) {
case "1":
// 查看所有學生
break;
case "2":
// 添加學生
break;
case "3":
// 刪除學生
break;
case "4":
// 修改學生
break;
case "5":
// 退出
// System.out.println("謝謝你的使用");
// break;
default:
System.out.println("謝謝你的使用");
System.exit(0); // JVM退出
break;
}
}
}
}
-
-
- 學生管理系統之查詢所有學生功能
- 案例代碼十一:
- 學生管理系統之查詢所有學生功能
-
package com.it.test;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 這是我的學生管理系統的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統的主界面的代碼編寫
* C:學生管理系統的查看所有學生的代碼編寫
* D:學生管理系統的添加學生的代碼編寫
* E:學生管理系統的刪除學生的代碼編寫
* F:學生管理系統的修改學生的代碼編寫
*/
public class StudentManagerTest {
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 sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用switch語句實現選擇
switch(choiceString) {
case "1":
//查看所有學生
findAllStudent(array);
break;
case "2":
//添加學生
break;
case "3":
//刪除學生
break;
case "4":
//修改學生
break;
case "5":
//退出
//System.out.println("謝謝你的使用");
//break;
default:
System.out.println("謝謝你的使用");
System.exit(0); //JVM退出
break;
}
}
}
//查看所有學生
public static void findAllStudent(ArrayList<Student> array) {
//首先來判斷集合中是否有數據,如果沒有數據,就給出提示,並讓該方法不繼續往下執行
if(array.size() == 0) {
System.out.println("不好意思,目前沒有學生信息可供查詢,請回去重新選擇你的操作");
return;
}
//\t 其實就是一個tab鍵的位置
System.out.println("學號\t\t姓名\t年齡\t居住地");
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
System.out.println(s.getId()+"\t"+s.getName()+"\t"+s.getAge()+"\t"+s.getAddress());
}
}
}
-
-
- 學生管理系統之添加學生功能
- 案例代碼十二:
- 學生管理系統之添加學生功能
-
package com.it;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 這是我的學生管理系統的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統的主界面的代碼編寫
* C:學生管理系統的查看所有學生的代碼編寫
* D:學生管理系統的添加學生的代碼編寫
* E:學生管理系統的刪除學生的代碼編寫
* F:學生管理系統的修改學生的代碼編寫
*/
public class StudentManagerTest {
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 sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用switch語句實現選擇
switch(choiceString) {
case "1":
//查看所有學生
break;
case "2":
//添加學生
addStudent(array);
break;
case "3":
//刪除學生
break;
case "4":
//修改學生
break;
case "5":
//退出
//System.out.println("謝謝你的使用");
//break;
default:
System.out.println("謝謝你的使用");
System.exit(0); //JVM退出
break;
}
}
}
/*
//添加學生
public static void addStudent(ArrayList<Student> array) {
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入學生學號:");
String id = 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.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//把學生對象作爲元素添加到集合
array.add(s);
//給出提示
System.out.println("添加學生成功");
}
*/
//添加學生
public static void addStudent(ArrayList<Student> array) {
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
//爲了讓id能夠被訪問到,我們就把id定義在了循環的外面
String id;
//爲了讓代碼能夠回到這裏,用循環
while(true) {
System.out.println("請輸入學生學號:");
//String id = sc.nextLine();
id = sc.nextLine();
//判斷學號有沒有被人佔用
//定義標記
boolean flag = false;
//遍歷集合,得到每一個學生
for(int x=0; x<array.size(); x++) {
Student s = array.get(x);
//獲取該學生的學號,和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
flag = true; //說明學號被佔用了
break;
}
}
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.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//把學生對象作爲元素添加到集合
array.add(s);
//給出提示
System.out.println("添加學生成功");
}
}
-
-
- 學生管理系統之刪除學生功能
- 案例代碼十三:
- 學生管理系統之刪除學生功能
-
package com.it;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 這是我的學生管理系統的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統的主界面的代碼編寫
* C:學生管理系統的查看所有學生的代碼編寫
* D:學生管理系統的添加學生的代碼編寫
* E:學生管理系統的刪除學生的代碼編寫
* F:學生管理系統的修改學生的代碼編寫
*/
public class StudentManagerTest {
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 sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用switch語句實現選擇
switch(choiceString) {
case "1":
//查看所有學生
break;
case "2":
//添加學生
break;
case "3":
//刪除學生
deleteStudent(array);
break;
case "4":
//修改學生
break;
case "5":
//退出
//System.out.println("謝謝你的使用");
//break;
default:
System.out.println("謝謝你的使用");
System.exit(0); //JVM退出
break;
}
}
}
//刪除學生
public static void deleteStudent(ArrayList<Student> array) {
//刪除學生的思路:鍵盤錄入一個學號,到集合中去查找,看是否有學生使用的是該學號,如果有就刪除該學生
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要刪除的學生的學號:");
String id = sc.nextLine();
/*
//遍歷集合
for(int x=0; x<array.size(); x++) {
//獲取到每一個學生對象
Student s = array.get(x);
//拿這個學生對象的學號和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
array.remove(x); //根據索引刪除
break;
}
}
//給出提示
System.out.println("刪除學生成功");
*/
//我們必須給出學號不存在的時候的提示
//定義一個索引
int index = -1;
//遍歷集合
for(int x=0; x<array.size(); x++) {
//獲取到每一個學生對象
Student s = array.get(x);
//拿這個學生對象的學號和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
index = x;
break;
}
}
if(index == -1) {
System.out.println("不好意思,你要刪除的學號對應的學生信息不存在,請回去重新你的選擇");
}else {
array.remove(index);
System.out.println("刪除學生成功");
}
}
}
-
-
- 學生管理系統之修改學生功能
- 案例代碼十四:
- 學生管理系統之修改學生功能
-
package com.it;
import java.util.ArrayList;
import java.util.Scanner;
/*
* 這是我的學生管理系統的主類
*
* 步驟如下:
* A:定義學生類
* B:學生管理系統的主界面的代碼編寫
* C:學生管理系統的查看所有學生的代碼編寫
* D:學生管理系統的添加學生的代碼編寫
* E:學生管理系統的刪除學生的代碼編寫
* F:學生管理系統的修改學生的代碼編寫
*/
public class StudentManagerTest {
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 sc = new Scanner(System.in);
String choiceString = sc.nextLine();
//用switch語句實現選擇
switch(choiceString) {
case "1":
//查看所有學生
break;
case "2":
//添加學生
break;
case "3":
//刪除學生
break;
case "4":
//修改學生
updateStudent(array);
break;
case "5":
//退出
//System.out.println("謝謝你的使用");
//break;
default:
System.out.println("謝謝你的使用");
System.exit(0); //JVM退出
break;
}
}
}
//修改學生
public static void updateStudent(ArrayList<Student> array) {
//修改學生的思路:鍵盤錄入一個學號,到集合中去查找,看是否有學生使用的是該學號,如果有就修改該學生
//創建鍵盤錄入對象
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你要修改的學生的學號:");
String id = sc.nextLine();
//定義一個索引
int index = -1;
//遍歷集合
for(int x=0; x<array.size(); x++) {
//獲取每一個學生對象
Student s = array.get(x);
//拿學生對象的學號和鍵盤錄入的學號進行比較
if(s.getId().equals(id)) {
index = x;
break;
}
}
if(index == -1) {
System.out.println("不好意思,你要修改的學號對應的學生信息不存在,請回去重新你的選擇");
}else {
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.setId(id);
s.setName(name);
s.setAge(age);
s.setAddress(address);
//修改集合中的學生對象
array.set(index, s);
//給出提示
System.out.println("修改學生成功");
}
}
}