集合出現的原因
數組存儲數據是固定存儲 ,當遇到要存儲數據的個數不確定的時候 數組就不滿足了,集合就出現了
集合存儲數據的個數,可以隨着數據量的變化而變化,不會造成越界或者大量的空間浪費
存儲數據的個數是可變的
ArrayList:
java.util包下
底層維護了一個數組
線程不同步(處理速度快)
創建ArrayList對象的格式:
ArrayList<E> 集合名字 = new ArrayList<E>();
<E>: 泛型, 代表了集合中要存儲的數據類型, 想存什么類型 就把E改成什么類型 , 如要存儲String類型的數據 就把E改成String
注意: 集合只能存儲引用類型的數據
基本數據類型 對應的引用數據類型表示形式
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
ArrayList常見功能
添加
public boolean add(E e)
public void add(int index,E element) // 在指定的索引位置添加元素
獲取元素
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)// 使用element 去替換指定索引的元素 , 並返回被替換的元素
學生管理系統練習
學生信息包括 : 學號 姓名 年齡 家鄉
打印歡迎語句
打印對應的功能 , 並接收用戶的輸入
1.查看學生信息
如果系統沒有學生信息 則給出對應的提示
如果系統中有學生信息, 則按照指定的格式打印學生信息
2.添加學生信息
從鍵盤錄入學生的信息 組成對象 添加到集合中
根據學號去重,只有沒重復的學號才能添加到集合中
3.修改學生信息
根據學號找到學生進行修改
如果沒有學號則給出對應的提示
如果找到學號則繼續收集新信息, 使用新信息修改原來的元素
4.刪除學生信息
根據學號刪除學生
如果沒有指定學號則給出指定的提示
如果有學號則刪除指定的元素
5.退出學生信息管理系統
提示退出
並結束程序
代碼演示
1 public static void main(String[] args) { 2 // 初始化數據 3 // 創建一個集合容器 可以存儲學生的信息 4 ArrayList<Student> list = new ArrayList<Student>(); 5 // =========================測試數據================================ 6 // Student s1 = new Student("9001", "阿拉甲", "18", "迪拜"); 7 // Student s2 = new Student("9002", "阿拉yi", "18", "迪拜"); 8 // Student s3 = new Student("9003", "阿拉餅", "18", "迪拜"); 9 // list.add(s1); 10 // list.add(s2); 11 // list.add(s3); 12 // System.out.println("初始化完畢"); 13 // =========================測試數據================================ 14 15 System.out.println("-------------------歡迎使用學生管理系統------------------------"); 16 17 // 死循環 18 while (true) { 19 // 展示功能菜單 20 System.out.println("================================="); 21 System.out.println("1.查看學生信息"); 22 System.out.println("2.添加學生信息"); 23 System.out.println("3.修改學生信息"); 24 System.out.println("4.刪除學生信息"); 25 System.out.println("5.退出學生信息管理系統"); 26 System.out.println("請輸入對應功能的序號"); 27 System.out.println("================================="); 28 // 接收用戶的輸入 29 Scanner sc = new Scanner(System.in); 30 int user = sc.nextInt(); 31 // 根據用戶的輸入進行功調用 32 switch (user) { 33 case 1: 34 show(list); 35 break; 36 case 2: 37 add(list); 38 break; 39 case 3: 40 upd(list); 41 break; 42 case 4: 43 del(list); 44 break; 45 case 5: 46 System.out.println("感謝使用管理系統 歡迎下次再來哦 "); 47 // 終止虛擬機 48 System.exit(0); 49 // return; 50 break; 51 52 default: 53 System.out.println("對不起 沒有這個功能 ,請控制你自己 "); 54 break; 55 } 56 } 57 } 58 59 // 功能方法s 60 public static void del(ArrayList<Student> list) { 61 // 1.提示輸入學號 62 Scanner sc = new Scanner(System.in); 63 System.out.println("請輸入學號"); 64 String id = sc.next(); 65 66 // 2.查找 67 // 定義標記 68 int index = -1; 69 // 遍歷比較 並修改 70 for (int i = 0; i < list.size(); i++) { 71 Student tmp = list.get(i); 72 if (tmp.getId().equals(id)) { 73 // 找到了 74 // 改變標記 75 index = i; 76 break; 77 } 78 } 79 // 3.判斷結果 80 // 判斷標記 81 if (index == -1) { 82 // 沒有找到 83 System.out.println("您輸入的學號 咱們系統沒有, 請重新選擇功能"); 84 } else { 85 // 找到了 執行刪除 86 list.remove(index); 87 System.out.println("刪除完畢"); 88 } 89 } 90 91 public static void upd(ArrayList<Student> list) { 92 // 1.提示輸入學號 93 Scanner sc = new Scanner(System.in); 94 System.out.println("請輸入學號"); 95 String id = sc.next(); 96 97 // 2.查找 98 // 定義標記 99 int index = -1; 100 // 遍歷並比較 101 for (int i = 0; i < list.size(); i++) { 102 Student tmp = list.get(i); 103 if (tmp.getId().equals(id)) { 104 // 找到了 105 // 修改標記 106 index = i; 107 break; 108 } 109 } 110 // 3.根據查找的結果做不同的動作 111 // 判斷標記 112 if (index == -1) { 113 // 沒找到, 114 System.out.println("您輸入的學號 咱們系統中沒有 ,請重新選擇功能 "); 115 } else { 116 // 找到了 117 // 3.收集其他信息 118 System.out.println("請輸入新姓名"); 119 String name = sc.next(); 120 System.out.println("請輸入新年齡"); 121 String age = sc.next(); 122 System.out.println("請輸入新家鄉"); 123 String home = sc.next(); 124 // 4.組成對象添加到集合中 125 Student s = new Student(id, name, age, home); 126 // 修改 127 list.set(index, s); 128 System.out.println("修改完畢"); 129 } 130 131 } 132 133 public static void add(ArrayList<Student> list) { 134 // 1.提示輸入學號 135 Scanner sc = new Scanner(System.in); 136 System.out.println("請輸入學號"); 137 String id = sc.next(); 138 // 2.根據學號去重 139 140 // 使用用戶輸入的學號去集合中查找, 如果找到與用戶輸入的學號一樣的學號表示有重復,此時要繼續提示輸入學號,並繼續去重 141 // 直到用戶輸入的學號與集合中元素的學號不一致的時候再收集其他的信息 142 while (true) { 143 // 定義一個標記 給一個默認值 144 int index = -1; 145 // 遍歷集合獲取元素的學號與用戶輸入的學號進行比較 146 for (int i = 0; i < list.size(); i++) { 147 Student tmp = list.get(i); 148 if (tmp.getId().equals(id)) { 149 // 表示重復 150 // 修改標記 151 index = i; 152 break; 153 } 154 } 155 156 // 判斷標記 157 if (index == -1) { 158 // 沒有重復 159 break; 160 } else { 161 // 有重復 162 System.out.println("您輸入的學號 重復了 ,請重新輸入學號 "); 163 id = sc.next(); 164 165 } 166 } 167 168 // 3.收集其他信息 169 System.out.println("請輸入姓名"); 170 String name = sc.next(); 171 System.out.println("請輸入年齡"); 172 String age = sc.next(); 173 System.out.println("請輸入家鄉"); 174 String home = sc.next(); 175 // 4.組成對象添加到集合中 176 Student s = new Student(id, name, age, home); 177 list.add(s); 178 System.out.println("添加完畢"); 179 } 180 181 public static void show(ArrayList<Student> list) { 182 // 1.判斷集合是否有元素 183 if (list.size() == 0) { 184 // 如果沒有給出特定的提示 185 System.out.println("系統中沒有學生的信息,請選擇添加功能"); 186 } else { 187 // 如果有就按照指定格式遍歷 188 System.out.println("================學生信息如下===================="); 189 System.out.println("學號\t\t姓名\t\t年齡\t\t家鄉"); 190 // 遍歷集合獲取學生信息 191 for (int i = 0; i < list.size(); i++) { 192 Student tmp = list.get(i); 193 System.out 194 .println(tmp.getId() + "\t\t" + tmp.getName() + "\t\t" + tmp.getAge() + "\t\t" + tmp.getHome()); 195 } 196 System.out.println("===================================="); 197 } 198 System.out.println("展示完畢"); 199 }