//There is a collection that determines if there is a "world" element in the collection, and if so, adds "Java ee"List List=NewArrayList (); List.add ("World"); List.add ("Java"); List.add ("Hello"); //concurrentmodificationexception / *Iterator it = List.iterator (); while (It.hasnext ()) {String s = (string) it.next (); if ("World". Equals (s)) {list.add ("Java ee");} }*///Method 1 iterates over the element, and the iterator modifies the element. element is appended to the element that was just iterated Listiterator lit=List.listiterator (); while(Lit.hasnext ()) {String s=(String) lit.next (); if("World". Equals (s)) {lit.add ("Java ee");} } System.out.println (list);//[World, EE, java, hello]Method 2 Sets the traversal element, the collection modifies the element (normal for),the element is added at the end for(intX=0;x<list.size (); x + +) {String s=(String) list.get (x); if("World". Equals (s)) {List.add ("Java EE"); }} System.out.println (list);//[World, EE, Java, hello, java EE]
There is a collection that determines if there is a "world" element in the collection, and if so, adds "Java ee"