How the list of Java collections is traversed

Source: Internet
Author: User

Learn how to iterate through the Java collection in a gradual manner:

First, take the list collection as an example:

Package com.test;

Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;

public class Testing {

public static void Main (string[] args) {
TODO auto-generated Method Stub

List<string> list=new arraylist<string> ();
List.add ("a");
List.add ("B");
List.add ("C");
List.add ("D");
List.add ("E");

Method One: (iterator (1))
For (iterator<string> iterator=list.iterator (); Iterator.hasnext ();) {
String St=iterator.next ();

System.out.println ("The string that iterates the output of the list of methods one is" +st);
}

Method Two: For loop (1)
for (int i=0;i<list.size (); i++) {
String St=list.get (i);
System.out.println ("string =" +st of the output of the list of the traversal method two);
}


Method Three: For Loop (2)
for (String st:list) {
System.out.println ("The string for the list output of the Traverse method three is" +st);
}

Method four-:(iterators (2))
Iterator<string> Iterator=list.iterator ();
while (Iterator.hasnext ()) {
String St=iterator.next ();
System.out.println ("Traversal method four list output string is" +st);

}
}

}

The output is as follows:

The string that iterates the output of the list of methods one is a
The string that iterates the output of the list of methods one is B
A string that iterates the output of the list of methods one is C
The string that iterates the output of the list of methods one is D
The string that iterates the output of the list of methods one is E
String =a of the output of the list traversing method two
String =b of the output of the list traversing method two
String =c of the output of the list traversing method two
String =d of the output of the list traversing method two
String =e of the output of the list traversing method two
The list output of the traversal method three is a string a
The list output of the Traverse method three is string b
The list output of the Traverse method Three is a string C
The list output of the Traverse method Three is a string of D
The string for the list output of the Traverse method three is E
Traversal method Four the list output of string is a
Traversal method Four the list output of string is B
Traversal method Four the list output of string is C
Traversal method Four the list output of string is D
Traversal method Four the list output of string is E

! ============ Record bit growth ============!

How the list of Java collections is traversed

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.