Ordinary for loop can be deleted, but the index for -
iterator can be deleted, but you must use the iterator's own remove method, otherwise there will be concurrent modification abnormal
enhanced for loop can not be deleted
Import of java.util.ArrayList; Import the java.util.Iterator; Import com.heima.bean.Person; public class demon_bigFor { / * * Enhanced for loop * implemented with underlying iterator * * Format: * for (element data type variable : set array or collection) { * can use variable, the variable element is *} * / public static void main (String [] args) { // the demo1 (); // demo2 (); the ArrayList <String> L3 = new new the ArrayList <> (); l3.add ( "A" ); l3.add ("b"); l3.add("c"); l3.add("d"); // 1,普通for循环 /*for (int i = 0; i < l3.size(); i++) { if ("b".equals(l3.get(i))) { l3.remove(i); } } System.out.println(l3);*/ // 2,迭代循环 Iterator<String> iterator = l3.iterator(); while (iterator.hasNext()) { String string = iterator.next(); if ("b".equals(string)) { iterator.remove(); } } } public static void demo2() { ArrayList<Person> l2 = new ArrayList<>(); l2.add(new Person("张三",23)); l2.add(new Person("李四",24)); l2.add(new Person("王五",25)); l2.add(new Person("赵柳",26)); l2.add(new Person("周七",27)); for (Person person : l2) { System.out.println(person); } } public static void demo1() { int[] arr = {11,22,33,44,55}; /*for (int i : arr) { System.out.println(i); }*/ ArrayList<String> l1 = new ArrayList<>(); l1.add("a"); l1.add("b"); l1.add("c"); l1.add("d"); for (String string : l1) { System.out.println(string); } } }