There is a collection that determines if there is a "world" element in the collection, and if so, adds "Java ee"

Source: Internet
Author: User

//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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.