public class Listtest {public
static void Main (string[] args) {
list<integer> List = new Arraylist<integ Er> ();
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);
}
}
SYSTEM.OUT.PRINTLN (list);
}
Output results: [1, 2, 3, 4]
The elements in the list are not all deleted, because after each remove an element, the following elements will move forward, causing the element that has just been moved is not read. So you can solve this problem by traversing backwards, the code is as follows:
for (int i = List.size ()-1; I >= 0; i--) {
System.out.println (i);
if (list.get (i) = = 3) {
list.remove (i);
}
}
It can also be deleted by the RemoveAll method, which is collection
list<integer> item = new arraylist<integer> ();
Item.add (3);
List.removeall (item);