I. list.remove (i)
public static void Main (string[] args) {list<integer> List = new arraylist<integer> (); List.add (1); List.add (2); List.add (3); List.add (3); List.add (4); for (int i=0; i<list.size (); i++) {if (List.get (i) = = 3) {list.remove (i);//This is not written, it needs to be written in List.remove (i -)}} System.out.println (list);}
Similarly,when a map is traversed by keyset () or EntrySet (), it cannot delete the element inside, otherwise it will be reported java.util.ConcurrentModificationException
To avoid this, you need to iterate through the list or map and delete it by using the iterator iterator, which is removed by Iterator.remove ().
two. Recommended use of the Iterator.remove method
if (null! = List && list.size () > 0) {Iterator it = list.iterator (); while (It.hasnext ()) {Student Stu = (Student) it.next (); if (stu.getstudentid () = = StudentID) {it.remove ();///Remove the object}}}
Java.util.List The Remove () method using the Tips