Requirements: I have a collection, such as the next, I would like to judge there is no "world" this element, if there is, I will add a "Java ee" element, please write code implementation.
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
public class Demo {
public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
List.add ("Hello");
List.add ("World");
List.add ("Java");
Iterator<string> it = List.iterator ();
while (It.hasnext ()) {
String s = it.next ();
if ("World". Equals (s)) {
List.add ("Java ee");
}
}
System.out.println ("list:" + list);
}
}
The above code will appear concurrentmodificationexception at run time: Concurrency modification exception
Causes of the production:
Iterators are dependencies and collections exist, after the success of the decision, the collection center added elements, and iterators do not know, so the error, which is called concurrent modification exception.
What this problem describes is that when an iterator iterates through an element, the element cannot be modified by a collection.
So what should you do when you encounter such an exception?
Way one: We can use iterators to iterate over elements, and iterators to add elements.
At this point we are using an iterator that is unique to the list collection listiterator the Listiterator () list iterator.
The iterator inherits the iterator iterator, so you can use the Hasnext () and Next () methods directly.
Additionally, the Listiterator void Add (Object obj) method can add elements.
Note: The elements added using the list iterator Listiterator are followed by the elements that were just iterated.
Then the code that uses the list iterator to complete the above requirements is reflected in the following:
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.ListIterator;
public class Demo {
public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
List.add ("Hello");
List.add ("World");
List.add ("Java");
Listiterator<string> lit = List.listiterator ();
while (Lit.hasnext ()) {
String s = lit.next ();
if ("World". Equals (s)) {
Lit.add ("Java ee");
}
}
System.out.println ("list:" + list);
}
}
Way two: Use the set to traverse elements, set to modify elements (using normal for)
Use the size () and get () methods of the list collection to iterate through the collection
Note that using normal for traversal and modify collection elements, the elements are all added at the last
Then the code that uses the normal for to complete the above requirements is reflected as:
Import java.util.ArrayList;
Import java.util.List;
public class Demo {
public static void Main (string[] args) {
list<string> list = new arraylist<string> ();
List.add ("Hello");
List.add ("World");
List.add ("Java");
for (int x = 0; x < list.size (); + +) {
String s = list.get (x);
if ("World". Equals (s)) {
List.add ("Java ee");
}
}
System.out.println ("list:" + list);
}
}
Concurrentmodificationexception: Concurrency Modification exception