First of all, this is a very simple question, Daniel can ignore, novice may encounter, Java in the traversal of a list when deleting the list element throws an exception.
This is a simple problem to master seriously insignificant, but novice may be more confused, in which way can be safely and effectively implement Traverse list Delete some of the feature elements?
Way:
Method 1, normal for loop traversal list, delete list in addition to its own feature entries;
Method 2, advanced for loop traversal list, delete list itself specific entries;
Method 3, Introduction of Reference list, forloop traversal Delete The original List specific entries ;
Method 4, using iterator traversal to delete list-specific entries
Conclusion:
Method 1: Do not throw an exception but the result may not be correct because list.size () is not necessarily a characteristic element in the change according to the foot mark;
method 2: Throw an exception : Java.util.ConcurrentModificationException;
Java.util.ConcurrentModificationException
At Java.util.arraylist$itr.checkforcomodification (arraylist.java:819)
At Java.util.arraylist$itr.next (arraylist.java:791)
method 3: can achieve the purpose, by introducing a reference list for loop traversal reference list in the loop process to meet the conditions to delete the original list entry, you can achieve the desired purpose ;
method 4: can achieve the purpose, through the List.iterator () method to get the iterator object call Iterator.remove (), the method can achieve the desired purpose and will not throw an exception.
It's best to run the code yourself, try it, and get a better understanding of it.
No nonsense, on the code, you can run a bit:
Package Com.kevin.test;import Java.util.arraylist;import Java.util.iterator;import java.util.list;import java.util.random;/** * Dynamic Delete List Feature element example * * @author BLJ * */public class testmain{static class Bean{private String name ;p ublic String getName () {return name;} public void SetName (String name) {this.name = name;}} /** * @param args */public static void main (string[] args) {Dynamicremoveitemfromlistdemo ();} /** * Dynamic Delete list element example */public static void Dynamicremoveitemfromlistdemo () {list<bean> list = GetList (); SYSTEM.OUT.PRINTLN ("raw Data:");p rintlist (list);//Use the normal for loop to iterate through the list to delete its own elements without error but the results may not be correct normalremovemethod (list);// Use the advanced for loop to traverse the list to delete its own element error//try//{//Superremovemethod (list),//}//catch (Exception e)//{//System.err.println (" Advanced for Loop error: ");//E.printstacktrace ()//}//Introduction of reference List traversal Delete method//Referenceremovemethod (list)//Iterator Traversal Delete method// Iteratorremovemethod (list); SYSTEM.OUT.PRINTLN ("Result data:");p rintlist (list);} /** * Print List method * * @param list */private static void Printlist (List<bean> List) {for (Bean item:list) {System.out.print (Item.getname () + "");} System.out.println ();} /** * Use normal for loop to iterate over delete element * * @param list */private static void Normalremovemethod (List<bean> list) {for (int i = 0; I & Lt List.size (); i++) {if (List.get (i). GetName () equals ("A")) {List.remove (List.get (i));}} /** * Using advanced for Loop traversal delete element (will report exception) * * @param list */private static void Superremovemethod (list<bean> list) {//Method one for (Bea N item:list) {if (Item.getname (). Equals ("A")) {List.remove (item);}}} /** * Valid for loop traversal delete element (normal to achieve intended purpose) * * @param list */private static void Referenceremovemethod (List<bean> list) {LIST&L T bean> referencelist = new arraylist<bean> () Referencelist.addall (list); for (int i = 0; i < referencelist.size (); i++) {if (Referencelist.get (i). GetName () equals ("A")) {List.remove (Referencelist.get (i));}} /** * can also be successfully removed and traversed using Iterator */public static void Iteratorremovemethod (list<bean> List) {iterator<bean> it = List.iterator (); while (It.hasnext ()) {Bean Student = It.next (); if (Student.getname (). Equals ("A")) It.remove ();}} /** * Get List Method * * @return */private static list<bean> getList () {list<bean> list = new Arraylist<bean> ( ); for (int i = 0; i <; i++) {Bean item = new bean (); int J = i + New Random (). Nextint (); if (j% 3 = = 1) {Item.setname (" A ");} else if (j% 3 = = 2) {item.setname ("B");} Else{item.setname ("C");} List.add (item);} return list;}}
Delete List feature element exception problem and solution summary when Java dynamically traverse list