The reason why sets appear
Arrays store data in fixed storage. When the number of data to be stored is uncertain, the array is not satisfied, and sets appear
Sets The number of stored data can change as the amount of data changes, without causing cross-border or a large amount of space waste
The number of stored data is variable
ArrayList:
Under the java.util package
The bottom layer maintains an array
The thread is not synchronized (fast processing speed)
Format of creating ArrayList object:
ArrayList< ;E> Collection name = new ArrayList
Note: Collections can only store reference type data
Basic data types Corresponding reference data type representation
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Common functions of ArrayList
Add
public boolean add(E e)
public void add(int index,E element) // Add at the specified index position Element
Get the element
public E get(int index)//Get the element based on the index value
Get the number of elements
public int size() //Get Number of elements
Delete elements
public boolean remove(Object o) //Delete elements directly
public E remove(int index) //Delete elements based on index and return the deleted elements
Modify elements
public E set(int index,E element)//Use element to replace the element at the specified index and return the replaced element
Student Management System Exercise
Student information includes: student ID, name, age, hometown
Print welcome statement
Print corresponding functions, and receive user input
1. View student information
If the system If there is no student information, the corresponding prompt will be given
If there is student information in the system, the student information will be printed in the specified format
2. Add student information
Enter the student's information from the keyboard to form an object and add it to the collection
Remove duplicates based on student numbers. Only non-duplicated student numbers can be added to the collection
3. Modify student information
Find the student based on the student number and modify it
If there is no student number, give Prompt the corresponding prompt
If the student number is found, continue to collect new information and use the new information to modify the original elements
4. Delete student information
Delete the student based on the student number
If no student number is specified, give Out the specified prompt
If there is a student number, delete the specified element
5. Exit the student information management system
Prompt to exit
and end the program
Code demonstration
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 }
The above is the detailed content of ArrayList collection. For more information, please follow other related articles on the PHP Chinese website!