JAVA集合
- 對象數組
- 集合類之ArrayList
- 學生管理系統
- 斗地主案例
NO.one 對象數組
1.1 對象數組描述
A:基本類型的數組:存儲的元素為基本類型
int[] arr={1,2,3,4}
B:對象數組:存儲的元素為引用類型
Student[] stus=new Student[3];
Student代表一個自定義類
Stus數組中stus[0],stus[1],stus[2]的元素數據類型為Student,
都可以指向一個Student對象
1.2 對象數組案例:
創建一個學生數組,存儲三個學生對象並遍歷
1.2.1 案例代碼一:
package com.gao; /* * 自動生成構造方法: * 代碼區域右鍵 -- 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()); } } }
1.3 對象數組的內存圖
NO.two 集合類ArrayList
2.1 集合概述
A:我們學習的是面向對象編程語言,而面向對象編程語言對事物的描述都是通過對象來體現的。
為了方便對多個對象進行操作,我們就必須對這多個對象進行存儲,而要想對多個對象進行存儲,
就不能是一個基本 的變量,而應該是一個容器類型的變量。
B:StringBuilder,數組。
StringBuilder的結果只能是一個字符串類型,不一定滿足我們的需求。
所以,我們目前只能選擇數組了,也就是我們前面學習過的對象數組。
但是,數組的長度是固定的, 如果有時候元素的個數不確定的,我們無法定義出數組的長度,這個時候,
java就提供了集合類供我們使用。
2.2 ArrayList集合
2.2.1 ArrayList 添加新元素
2.2.1.1 案例代碼二:
package com.gao_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); } }
2.2.2 刪改查方法
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):修改指定索引處的元素,返回被修改的元素
2.2.2.1 案例代碼三:
package com.gao_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); } }
2.2.3 ArrayList遍歷
集合的遍歷思想和數組的遍歷思想相同
循環遍歷容器,依次取出里面的元素即可
2.2.3.1 案例代碼四:
package com.gao_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); } } }
2.3 ArrayList集合案例
2.3.1 ArrayList 練習之存儲字符串並遍歷
向集合中添加任意四個字符串,遍歷集合,依次打印取出的字符串
2.3.1.1 案例代碼五:
package com.gao_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); } } }
2.3.2 ArrayList 練習之獲得滿足要求的元素
給定一個字符串數組:{“張三豐”,“宋遠橋”,“張無忌”,“殷梨亭”“張翠山”,“莫聲谷”},將數組中的元素添加到集合中,並把所有姓張的人員打印到控制台上
2.3.2.1 案例代碼六:
package com.gao_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); } } } }
2.3.3 ArrayList 練習之存儲自定義對象並遍歷
A:自定義一個學生類,學生中有姓名和年齡屬性,生成滿參構造與空參構造
生成屬性對應的getter/setter方法
B:在測試類中使用滿參構造創建三個學生對象,然后將每個學生對象均添加到ArrayList集合中
C:遍歷這個ArrayList集合,依次打印出每個學生的姓名和年齡
2.3.3.1 案例代碼七:
package com.gao_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()); } } }
2.3.4 ArrayList 練習之鍵盤錄入數據儲存並遍歷
創建一個Student類包含姓名和年齡屬性
創建一個ArrayList集合
向集合中添加三個Student對象Student對象中姓名和年齡的數據均來自與鍵盤錄入
最終遍歷這個集合,取出Student對象以及里面屬性的值
2.3.4.1 案例代碼八:
package com.gao_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); } }
NO.three 學生管理系統案例
3.1 學生管理系統案例需求
-------------歡迎來到學生管理系統--------------
1 查看所有學生
2 添加學生
3 刪除學生
4 修改學生
5 退出
請輸入您的選擇:
利用集合完成對學生的怎刪改查四個功能
3.2 學生管理系統案例實現
3.2.1 創建學生類
3.2.1.1 案例代碼九:
package com.gao; /* * 這是我的學生類 */ 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; } }
3.2.2 學生管理系統界面實現
3.2.2.1 案例代碼十:
package com.gao; 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; } } } }
3.2.3 學生管理系統之查詢所有學生功能
3.2.3.1 案例代碼十一:
package com.gao.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()); } } }
3.2.4 學生管理系統之添加學生功能
3.2.4.1 案例代碼十二:
package com.gao; 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("添加學生成功"); } }
3.2.5 學生管理系統之刪除學生功能
3.2.5.1 案例代碼十三:
package com.gao; 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("刪除學生成功"); } } }
3.2.6 學生管理系統之修改學生功能
3.2.6.1 案例代碼十四:
package com.gao; 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("修改學生成功"); } } }
3.2.7 學生管理系統之完整的程序:
3.2.7.1 完整代碼
package com.gao; import java.util.ArrayList; import java.util.Scanner; public class StudentDemo { public static void main(String[] args) { // 創建集合對象,儲存學生對象 ArrayList<Student> array = new ArrayList<Student>(); // 主界面 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("請輸入你的選擇"); while (true){ // 多次輸入命令 Scanner in = new Scanner(System.in); int num = in.nextInt(); switch (num){ case 1: viewAllStudent(array); break; case 2: addStudent(array); break; case 3: deleteStudent(array); break; case 4: updateStudent(array); break; default : System.out.println("謝謝您的使用。"); System.exit(0); break; } } } // 1 查看所有學生信息 public static void viewAllStudent(ArrayList<Student> array) { if (array.size() == 0){ System.out.println("不好意思,目前還沒有學生信息可供查詢。"); return ; } System.out.println("學號\t姓名\t年齡\t居住地"); for (int i = 0; i < array.size(); i ++) { Student s = array.get(i); System.out.println(s.getId()+"--"+s.getName()+"--"+s.getAge()+"--"+s.getAddress()); } } // 2 添加學生信息 public static void addStudent(ArrayList<Student> array){ Scanner in = new Scanner(System.in); String id; while (true){ id = in.nextLine(); boolean flag = false; for (int i = 0; i < array.size(); i ++){ Student s = array.get(i); if (s.getId().equals(id)){ flag = true; break; } } if (flag) System.out.println("你輸入的學號已被使用,請重新輸入。"); else break; } String name = in.nextLine(); String age = in.nextLine(); String address = in.nextLine(); Student s = new Student(); s.setId(id); s.setName(name); s.setAge(age); s.setAddress(address); array.add(s); System.out.println("成功添加學生信息。"); } // 3 刪除學生信息 public static void deleteStudent(ArrayList<Student> array) { Scanner in = new Scanner(System.in); System.out.println("請輸入您要刪除的學生的學號:"); String id = in.nextLine(); int index = -1; for (int i = 0; i < array.size(); i ++){ Student s = array.get(i); if (s.getId().equals(id)){ index = i; break; } } if (index == -1) System.out.println("不好意思,你要刪除的學生信息已不存在,請重新輸入。"); else{ array.remove(index); System.out.println("成功刪除。"); } } // 4 修改學生信息 public static void updateStudent(ArrayList<Student> array){ Scanner in = new Scanner(System.in); System.out.println("請輸入要修改的學生信息。"); String id = in.nextLine(); int index = -1; for (int i = 0; i < array.size(); i ++){ Student s = array.get(i); if (s.getId().equals(id)){ index = i; break; } } if (index == -1) System.out.println("不好意思,你要修改的學生信息不存在,請重新輸入。"); else{ String name = in.nextLine(); String age = in.nextLine(); String address = in.nextLine(); Student s = new Student(); s.setId(id); s.setName(name); s.setAge(age); s.setAddress(address); array.set(index, s); System.out.println("修改信息成功。"); } } }
NO.four 斗地主案例
具體規則:
1. 組裝54張撲克牌
2. 將54張牌順序打亂
3. 三個玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。
4. 查看三人各自手中的牌、底牌
案例代碼:
package com.gao_03; import java.util.ArrayList; import java.util.Collections; /* * 模擬斗地主發牌 買牌 洗牌 發牌 */ public class CollectionsTest { public static void main(String[] args) { //買牌 String[] arr = {"黑桃","紅桃","方片","梅花"}; String[] arr2 = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; ArrayList<String> box = new ArrayList<String>(); //添加每張牌 for (int i = 0; i < arr.length; i++) { //獲取每一個花色 for (int j = 0; j < arr2.length; j++) { //獲取每一個數 box.add(arr[i] + arr2[j]); } } box.add("大王"); box.add("小王"); //System.out.println(box.size()); //洗牌 Collections.shuffle(box); //System.out.println(box); //發牌 ArrayList<String> 林志玲 = new ArrayList<String>(); ArrayList<String> 林心如 = new ArrayList<String>(); ArrayList<String> 舒淇 = new ArrayList<String>(); //留三張底牌給地主 for (int i = 0; i < box.size() - 3; i++) { /* * i = 0;i % 3 = 0; * i = 1;i % 3 = 1; * i = 2;i % 3 = 2; * i = 3;i % 3 = 0; * i = 4;i % 4 = 1; * i = 5;i % 5 = 2; */ if(i % 3 == 0) { 林志玲.add(box.get(i)); } else if(i % 3 == 1) { 林心如.add(box.get(i)); } else if(i % 3 == 2) { 舒淇.add(box.get(i)); } } System.out.println("林志玲:" + 林志玲); System.out.println("林心如:" + 林心如); System.out.println("舒淇:" + 舒淇); System.out.println("底牌:"); /* System.out.println(box.get(box.size() - 1)); System.out.println(box.get(box.size() - 2)); System.out.println(box.get(box.size() - 3));*/ for (int i = box.size() - 3; i < box.size(); i++) { System.out.println(box.get(i)); } } }