The principle is to delete the old elements before adding new ones, not to modify the old elements directly
package demo; /* * Courses */ public class course { public String id; public String name; public course (String id,String name) { this.id=id; this.name=name; } }
package demo; import java.util.HashSet; import java.util.Set; /** * Students * */ public class student { public String id; public String name; public Set course; public student (String id,String name) { this.id=id; this.name=name; this.course=new HashSet();//list is an interface class and cannot be instantiated. You can instantiate and create an object with the HashSet method } }
package demo; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /* * Store alternative courses */ public class ListTest { public List courseToSelect;// list container for alternate courses public ListTest() { this.courseToSelect = new ArrayList();//list cannot be instantiated, but its subclasses can be instantiated } /* * For adding alternative courses */ public void testAdd() { // Using two add methods to add elements to a set // Element add add method 1 course cr1 = new course("1", "data structure");// Create course object courseToSelect.add(cr1);// Call the add method to pass in the course object /* * After the course is added successfully, the feedback information needs to be printed to the user. The feedback information needs to be extracted before calling the print statement */ // get method is used to take the object storing information out of the container. 0 represents the subscript number. The object in the container is of object type, so cast is required course temp = (course) courseToSelect.get(0);// Takes the specified element and casts it System.out.println("Course successfully added:" + temp.id + "." + temp.name); // Element add method 2 course cr2 = new course("2", "C language");// Create course object courseToSelect.add(0, cr2);// Add the object cr2 containing course information to the specified location using the add method course temp2 = (course) courseToSelect.get(0);// Take out the element at the specified position in the container System.out.println("Course successfully added:" + temp2.id + "." + temp2.name); //Add the element repeatedly, the compiler does not report an error, indicating that it can be added repeatedly. course cr0 = new course("2", "C language");// Create course object courseToSelect.add(0, cr2);// Add the object cr2 containing course information to the specified location using the add method course temp0 = (course) courseToSelect.get(0);// Take out the element at the specified position in the container System.out.println("Course successfully added:" + temp2.id + "." + temp2.name); /* * Attempt to add object to non-existent element location */ /* * course cr3 = new course("3", "eclipse");// Create course object courseToSelect.add(4, * cr3);// Add the object cr2 containing course information to the specified location using the add method to run a bug. Bug reason: array subscript out of bounds */ // Using two addAll methods to add elements // The first method, addAll, adds the whole collection, and only the list class can be added, so you need to convert the course class to the list class before adding course[] course = { new course("3", "discrete mathematics "), new course("4", "assembly language") };// Create an array of 2 course objects courseToSelect.addAll(Arrays.asList(course));// Convert the course array to the list class and add it course temp3 = (course) courseToSelect.get(2);// Take out elements course temp4 = (course) courseToSelect.get(3);// Take out elements System.out.println("Two courses were successfully added:" + temp3.id + "." + temp3.name + ";" + temp4.id + "." + temp4.name); // The second addAll method course[] course2 = { new course("5", "Advanced mathematics"), new course("6", "College English") }; courseToSelect.addAll(2,Arrays.asList(course2));//Add to the specified location after turning course2 to list course temp5 = (course) courseToSelect.get(2);// Take out elements course temp6 = (course) courseToSelect.get(3);// Take out elements System.out.println("Two courses were successfully added:" + temp5.id + "." + temp5.name + ";" + temp6.id + "." + temp6.name); } /* * Get all elements in the list */ public void testGet() { int size= courseToSelect.size();//Get the length of the list collection System.out.println("There are the following courses to choose:"); for(int i=0;i<size;i++) { course fo = (course) courseToSelect.get(i); System.out.println( fo.id + "." + fo.name); } } /* * Go through the list through iterator */ public void testterator() { Iterator it=courseToSelect.iterator();//Iterator is also an interface class, which can only be instantiated by calling its subclass iterator through list System.out.println("There are the following courses to choose(Access via iterator): "); while(it.hasNext()) {//If the hasNext method determines whether there are any elements in the list, it returns the true value course cr=(course) it.next();//Use next method with Iterator to calendar list elements System.out.println( cr.id + "." + cr.name); } } /* * Iterators are used to go through the collection elements and cannot store any data */ /* * Accessing collection elements through the for each method */ public void testForEach(){ System.out.println("There are the following courses to choose(adopt for each Visit): "); for(Object obj:courseToSelect) {//Define an Object type variable obj, that is, go through all elements in the list course er=(course)obj;//Take out the element. When a course is saved in, the type is ignored. When it is taken out, type conversion is required System.out.println( er.id + "." + er.name); } } /* * Use the set method to modify the information in the specified element */ public void set() { courseToSelect.set(1, new course("7","Marxist Philosophy")); } // Create main method test run code public static void main(String[] args) { ListTest lt = new ListTest(); lt.testAdd(); lt.set(); lt.testGet(); lt.testterator(); lt.testForEach(); } }
Operation result:
Course successfully added: 1. Data structure
Successfully added course: 2.C language
Successfully added course: 2.C language
Two courses have been successfully added: 1. Data structure; 3. Discrete Mathematics
Two courses have been successfully added: 5. Advanced mathematics; 6. College English
There are the following courses to choose:
2.C language
7. Marxist Philosophy
5. Advanced Mathematics
6. College English
1. Data structure
3. Discrete Mathematics
4. Assembly language
There are the following courses to be selected (accessed through iterator):
2.C language
7. Marxist Philosophy
5. Advanced Mathematics
6. College English
1. Data structure
3. Discrete Mathematics
4. Assembly language
There are the following courses to be selected (accessed through for each):
2.C language
7. Marxist Philosophy
5. Advanced Mathematics
6. College English
1. Data structure
3. Discrete Mathematics
4. Assembly language