1.ArrayList
1.1ArrayList class overview [understanding]
-
What is a collection
A storage model with variable storage space is provided, and the stored data capacity can be changed
-
Characteristics of ArrayList set
The bottom layer is implemented by array, and the length can be changed
-
Use of generics
The data type used to constrain the storage elements in the collection
1.2 common methods of ArrayList [application]
1.2.1 construction method
Method name | explain |
---|---|
public ArrayList() | Create an empty collection object |
1.2.2 membership method
Method name | explain |
---|---|
public boolean remove(Object o) | Delete the specified element and return whether the deletion is successful |
public E remove(int index) | Deletes the element at the specified index and returns the deleted element |
public E set(int index,E element) | Modify the element at the specified index and return the modified element |
public E get(int index) | Returns the element at the specified index |
public int size() | Returns the number of elements in the collection |
public boolean add(E e) | Appends the specified element to the end of this collection |
public void add(int index,E element) | Inserts the specified element at the specified location in this collection |
1.2.3 example code
public class ArrayListDemo02 { public static void main(String[] args) { //Create collection ArrayList<String> array = new ArrayList<String>(); //Add element array.add("hello"); array.add("world"); array.add("java"); //public boolean remove(Object o): deletes the specified element and returns whether the deletion was successful // System.out.println(array.remove("world")); // System.out.println(array.remove("javaee")); //public E remove(int index): deletes the element at the specified index and returns the deleted element // System.out.println(array.remove(1)); //IndexOutOfBoundsException // System.out.println(array.remove(3)); //public E set(int index,E element): modifies the element at the specified index and returns the modified element // System.out.println(array.set(1,"javaee")); //IndexOutOfBoundsException // System.out.println(array.set(3,"javaee")); //public E get(int index): returns the element at the specified 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)); //?????? Self test //public int size(): returns the number of elements in the collection System.out.println(array.size()); //Output set System.out.println("array:" + array); } }
1.3 ArrayList stores strings and traverses [application]
1.3.1 case requirements
Create a collection of stored strings, store 3 string elements, and use the program to traverse the collection on the console
1.3.2 code implementation
/* Idea: 1:Create collection object 2:Adds a string object to the collection 3:To traverse the collection, you must first be able to get each element in the collection, which is implemented through the get(int index) method 4:After traversing the set, you should be able to obtain the length of the set, which is realized through the size() method 5:General format for traversing collections */ public class ArrayListTest01 { public static void main(String[] args) { //Create collection object ArrayList<String> array = new ArrayList<String>(); //Adds a string object to the collection array.add("Liu Zhengfeng"); array.add("Zuo lengchan"); array.add("Breezy"); //After traversing the set, you should be able to obtain the length of the set, which is realized through the size() method // System.out.println(array.size()); //General format for traversing collections for(int i=0; i<array.size(); i++) { String s = array.get(i); System.out.println(s); } } }
1.4ArrayList stores student objects and traverses [application]
1.4.1 case requirements
Create a collection of student objects, store 3 student objects, and use the program to traverse the collection on the console
1.4.2 code implementation
/* Idea: 1:Define student classes 2:Create collection object 3:Create student object 4:Add student object to collection 5:Traversal set, using the general traversal format */ public class ArrayListTest02 { public static void main(String[] args) { //Create collection object ArrayList<Student> array = new ArrayList<>(); //Create student object Student s1 = new Student("Lin Qingxia", 30); Student s2 = new Student("Breezy", 33); Student s3 = new Student("Zhang Manyu", 18); //Add student object to collection array.add(s1); array.add(s2); array.add(s3); //Traversal set, using the general traversal format for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } }
1.5ArrayList stores student objects and traverses the upgraded version [application]
1.5.1 case requirements
Create a collection of student objects, store 3 student objects, and use the program to traverse the collection on the console
The student's name and age are entered from the keyboard
1.5.2 code implementation
/* Idea: 1:Define the student class. For the convenience of keyboard data entry, all member variables in the student class are defined as String type 2:Create collection object 3:Enter the data required by the student object with the keyboard 4:Create a student object and assign the data entered by the keyboard to the member variable of the student object 5:Add a student object to the collection 6:Traversal set, using the general traversal format */ public class ArrayListTest { public static void main(String[] args) { //Create collection object ArrayList<Student> array = new ArrayList<Student>(); //In order to improve the reusability of code, we use methods to improve the program addStudent(array); addStudent(array); addStudent(array); //Traversal set, using the general traversal format for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } /* Two clear: Return value type: void Parameters: ArrayList < student > array */ public static void addStudent(ArrayList<Student> array) { //Enter the data required by the student object with the keyboard Scanner sc = new Scanner(System.in); System.out.println("Please enter student name:"); String name = sc.nextLine(); System.out.println("Please enter student age:"); String age = sc.nextLine(); //Create a student object and assign the data entered by the keyboard to the member variable of the student object Student s = new Student(); s.setName(name); s.setAge(age); //Add a student object to the collection array.add(s); } }
2. Student management system
2.1 implementation steps of student management system [understanding]
-
Case requirements
According to what we have learned at present, complete a comprehensive case: student management system! The main functions of the system are as follows:
Add student: enter student information through the keyboard and add it to the collection
Delete student: enter the student number of the student to be deleted through the keyboard to delete the student object from the set
Modify student: enter the student number of the student to be modified through the keyboard to modify other information of the student object
View student: display the student object information in the collection
Exit the system: end the program
-
Implementation steps
-
Define a student class that contains the following member variables
private String sid / / student id
private String name / / student name
private String age / / student age
private String address / / student location
-
Steps to build the main interface of student management system
2.1 write the main interface with output statements
2.2 keyboard input with Scanner
2.3 use switch statement to complete the function of selection
2.4 after completing the function with cycle, return to the main interface again -
Implementation steps of adding student functions to student management system
3.1 define a method to receive the ArrayList set
3.2 complete the function of adding students in the method
① input student information with keyboard
② create a student object according to the entered information
③ add the student object to the collection
④ prompt for adding success information
3.3 call the method of adding students in the option of adding students -
Implementation steps of viewing student function in student management system
4.1 define a method to receive the ArrayList set
4.2 traverse the set in the method and output the student information
4.3 call the view student method in the view all students option -
Implementation steps of deleting student function in student management system
5.1 define a method to receive the ArrayList set
5.2 receive the student number of the student to be deleted in the method
5.3 traverse the collection and get each student object
5.4 compare the student number of the student object with the entered student number to be deleted. If it is the same, the current student object will be deleted from the set
5.5 call the method of deleting students in the delete students option -
Modification of student management system and implementation steps of student function
6.1 define a method to receive the ArrayList set
6.2 the student number of the student to be modified is received in the method
6.3 input the information required by the student object through the keyboard and create the object
6.4 traverse the collection and get each student object. And compare it with the entered modified student number. If it is the same, the new student object will be used to replace the current student object
6.5 call the method of modifying students in the modify students option -
Exit the system
Use System.exit(0); Exit JVM
-
2.2 definition of student [application]
public class Student { //Student number private String sid; //full name private String name; //Age private String age; //Place of residence private String address; public Student() { } public Student(String sid, String name, String age, String address) { this.sid = sid; this.name = name; this.age = age; this.address = address; } 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 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; } }
2.3 definition of test class [ application ]
public class StudentManager { /* 1:Write the main interface with output statements 2:Keyboard data entry with Scanner 3:Complete the selection of operation with switch statement 4:Complete the cycle and return to the main interface again */ public static void main(String[] args) { //Create a collection object to save student data information ArrayList<Student> array = new ArrayList<Student>(); //Complete the cycle and return to the main interface again while (true) { //Write the main interface with output statements System.out.println("--------Welcome to the student management system--------"); System.out.println("1 Add student"); System.out.println("2 Delete student"); System.out.println("3 Modify student"); System.out.println("4 View all students"); System.out.println("5 sign out"); System.out.println("Please enter your choice:"); //Keyboard data entry with Scanner Scanner sc = new Scanner(System.in); String line = sc.nextLine(); //Complete the selection of operation with switch statement switch (line) { case "1": addStudent(array); break; case "2": deleteStudent(array); break; case "3": updateStudent(array); break; case "4": findAllStudent(array); break; case "5": System.out.println("Thank you for using"); System.exit(0); //JVM exit } } } //Define a method for adding student information public static void addStudent(ArrayList<Student> array) { //Enter the data required by the student object on the keyboard, display the prompt information, and prompt what information to enter Scanner sc = new Scanner(System.in); String sid; while (true) { System.out.println("Please enter student ID:"); sid = sc.nextLine(); boolean flag = isUsed(array, sid); if (flag) { System.out.println("The student number you entered has been occupied. Please re-enter it"); } else { break; } } System.out.println("Please enter student name:"); String name = sc.nextLine(); System.out.println("Please enter student age:"); String age = sc.nextLine(); System.out.println("Please enter student residence:"); String address = sc.nextLine(); //Create a student object and assign the data entered by the keyboard to the member variable of the student object Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //Add student object to collection array.add(s); //Give a prompt of successful addition System.out.println("Student added successfully"); } //Define a method to judge whether the student number is used public static boolean isUsed(ArrayList<Student> array, String sid) { //If it is the same as a student ID in the set, return true; If they are different, false is returned boolean flag = false; for(int i=0; i<array.size(); i++) { Student s = array.get(i); if(s.getSid().equals(sid)) { flag = true; break; } } return flag; } //Define a method for viewing student information public static void findAllStudent(ArrayList<Student> array) { //Judge whether there is data in the set. If no prompt is displayed if (array.size() == 0) { System.out.println("No information, please add information before querying"); //In order to stop the program from executing further, we write return here; return; } //Display header information //\t is actually the location of a tab key System.out.println("Student number\t\t\t full name\t\t Age\t\t Place of residence"); //Take out the data in the set, display the student information according to the corresponding format, and display the supplementary "age" for the age for (int i = 0; i < array.size(); i++) { Student s = array.get(i); System.out.println(s.getSid() + "\t" + s.getName() + "\t" + s.getAge() + "year\t\t" + s.getAddress()); } } //Define a method to delete student information public static void deleteStudent(ArrayList<Student> array) { //Enter the student number to be deleted on the keyboard and display the prompt information Scanner sc = new Scanner(System.in); System.out.println("Please enter the student number of the student you want to delete:"); String sid = sc.nextLine(); //Judge whether the student number exists before deleting / modifying student operations //If it does not exist, a prompt message is displayed //If it exists, delete / modify it int index = -1; for (int i = 0; i < array.size(); i++) { Student s = array.get(i); if (s.getSid().equals(sid)) { index = i; break; } } if (index == -1) { System.out.println("The information does not exist, please re-enter"); } else { array.remove(index); //Prompt for successful deletion System.out.println("Delete student succeeded"); } } //Define a method for modifying student information public static void updateStudent(ArrayList<Student> array) { //Enter the student number to be modified on the keyboard and display the prompt information Scanner sc = new Scanner(System.in); System.out.println("Please enter the student number of the student you want to modify:"); String sid = sc.nextLine(); //Enter the student information to be modified with the keyboard System.out.println("Please enter the new student name:"); String name = sc.nextLine(); System.out.println("Please enter the new age of the student:"); String age = sc.nextLine(); System.out.println("Please enter the student's new residence:"); String address = sc.nextLine(); //Create student object Student s = new Student(); s.setSid(sid); s.setName(name); s.setAge(age); s.setAddress(address); //Traverse the set and modify the corresponding student information for (int i = 0; i < array.size(); i++) { Student student = array.get(i); if (student.getSid().equals(sid)) { array.set(i, s); } } //Prompt for successful modification System.out.println("Modify student successfully"); } }