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