Package testlist;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.TreeSet;
public class Testiterator {
/**
* @param args
*/
public static void Main (string[] args) {
TODO auto-generated Method Stub
list<string> list = new arraylist<string> ();
List.add ("AAA");
List.add ("BBB");
List.add ("CCC");
Iterator T = list.iterator ();
while (T.hasnext ()) {
if (T.next (). Equals ("BBB")) {///when traversing the list collection using iterator, if you want to delete any of the set elements, you must remove them when you are traversing the penultimate element exactly.
List.remove ("CCC");
}
}
treeset<string> set = new treeset<string> ();
Set.add ("ddd");
Set.add ("Eee");
Set.add ("fff");
iterator T1 = Set.iterator ();
while (T1.hasnext ()) {
if (T1.next (). Equals ("fff")) {//////If you want to delete any of the set elements, you must delete them when you are traversing the last first element, if you are using iterator to traverse the set collection.
Set.remove ("Eee");
}
}
}
}
Because there is a certain difference in the implementation of the iterator, List,set has a different representation of the List,set to delete the set elements while iterating.