List Collection index traversal and iterator traversal in Java

Source: Internet
Author: User

Package Yzhou.iterator;import Java.util.arraylist;import java.util.hashset;import java.util.iterator;import Java.util.list;import Java.util.Set; Public classtest01{ Public Static voidMain (string[] args) {List List=NewArrayList (); List.add ("AAA"); List.add ("BBB"); List.add ("CCC"); //Traverse list by index         for(intI=0; I<list.size (); i++) {System. out. println (list.Get(i)); }                //Iterating through the list through an iterator         for(Iterator iter2 =list.iterator (); Iter2.hasnext ();) {String str=(String) iter2.next (); System. out. println (str);        Iter2.remove (); } System. out. println (List.size () +"************************************"); SetSet=NewHashSet (); Set. Add ("Zhangsan1"); Set. Add ("zhangsan2"); Set. Add ("zhangsan3"); //Iterator iter = Set.iterator ();//While (Iter.hasnext ())//        {//string str = (string) iter.next ();//System.out.println (str);//        }                 for(Iterator iter =Set. iterator (); Iter.hasnext ();) {String str=(String) iter.next (); System. out. println (str); }    }}

Separately:

 for Set . iterator (); Iter.hasnext ();)        {            = (String) Iter.next ();            System.  out . println (str);        }

This is for and iterator.

For the execution steps:

Initialize values First

Judge

Executing function body

Assignment ++/--

Judge

Executing function body

......

List Collection index traversal and iterator traversal in Java

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.