Iterator
(Iterator)
Author:
Zccst
Java. util. iterator
The interface describes the traversal of various set elements in a unified manner.
/
An Iteration tool, also known as an iterator ".
Iterator (
Iterator
) Mode, also called cursor (
Cursor
Is a standard access method used to traverse collection classes.
Gof
The definition is as follows: provide a method to access a container (
Container
), Without exposing the internal details of the object.
Java. util. iterator
Interface Definition:
Public interface iterator {
Boolean hasnext ();
//
Whether there is one next?
True
Otherwise, return
False
Object next ();
//
Traverse to the next
Void remove ();
//
Delete current element
}
It is easy to see that the elements in the Set (the one currently traversed) can be removed during the "traversal" process.
In fact, the traversal can be completed by relying on the first two methods. The typical code is as follows:
For (iterator it = C. iterator (); it. hasnext ();){
Object o = it. Next ();
//
Pair
O
Operations
...
}
Returned by each collection class
Iterator
The specific types may be different,
Array
Possible return
Arrayiterator
,
Set
Possible return
Setiterator
,
Tree
Possible return
Treeiterator
But they all implement
Iterator
Therefore, the client does not care which
Iterator
, It only needs to obtain
Iterator
Interface, which is the advantage of object-oriented.
Note: if not
Iterator
To traverse an array, use the index:
For (INT I = 0; I <array. Size (); I ++) {... get (I )...}
And access a linked list (
Shortlist
) And must be used.
While
Loop:
While (E = E. Next ())! = NULL) {... e. Data ()...}
The clients of the above two methods must know the internal structure of the set in advance. The access code and the set itself are tightly coupled and the access logic cannot be separated from the Collection class and client code, each set corresponds to a Traversal method, and client code cannot be reused. In addition, if you need
Arraylist
Change
Shortlist
The original client code must be overwritten.
For example
1
:
Testiterator. Java
Import java. util. date;
Import java. util. arraylist;
Import java. util. vector;
Import java. util. iterator;
Public class testiterator {
Public
Static void main (string [] ARGs ){
Arraylist
A = new arraylist ();
A. Add ("China ");
A. Add ("USA ");
A. Add ("Korea ");
Iterator
It = A. iterator ();
//
Use
It
Complete Traversal
While (it. hasnext ()){
String
Country = (string) it. Next ();
//
In this way
String
Type
System. Out. println (country );
}
Vector
V = new vector ();
V. addelement (New
Date ());
V. addelement (New
Date (200008755554l ));
It
= V. iterator ();
While (it. hasnext ()){
Date
Time = (date) it. Next ();
System. Out. println (time );
}
}
}
Output result:
China
USA
Korea
Sun Dec 27 18:53:27 CST 2009
Tue May 04 05:59:15 CST 1976
For example
2
:
Person. Java
,
Testiterator2.java
Note: In this example, there are two files that can be directly used if they are placed in the same package.
Import
.
(1) person. Java
Package test. SRC. test;
//
This is my
Person. Java
File package, which needs to be modified according to the actual situation
Public class person {
Private
String name;
Private
Int age;
Public
Person (string name, int age ){
This. Name
= Name;
This. Age
= Age;
}
Public
Void setname (string name ){
This. Name
= Name;
}
Public
String getname (){
Return
Name;
}
Public
Void setage (INT age ){
This. Age
= Age;
}
Public
Int getage (){
Return
Age;
}
Public
String tostring (){
Return
"Name:" + name + "/Tage:" + age;
}
}
(2) testiterator2.java
Package test. SRC. test;
//
Similarly, it needs to be modified according to the actual situation.
Import java. util. vector;
Import java. util. iterator;
Public class testiterator2 {
Public
Static void main (string [] ARGs ){
Vector
V = new vector ();
V. addelement (New
Person ("
Zhang San
", 18 ));
V. addelement (New
Person ("
Li Si
", 26 ));
V. addelement (New
Person ("
Wang Wu
", 34 ));
V. addelement (New
Person ("
Zhao Liu
", 40 ));
Iterator
It = V. iterator ();
While (it. hasnext ()){
// While
Cyclically output each element
Person
P = (person) it. Next ();
System. Out. println (P );
If (P. getname (). Equals ("
Wang Wu
")){
P. setage (P. getage ()
+ 50 );
} Else
If (P. getname (). Equals ("
Li Si
")){
It. Remove ();
}
}
System. Out. println ("-----------------------");
For (int
I = 0; I <v. Size (); I ++)
System. Out. println (V. elementat (I ));
}
}
Output result:
Name:
Zhang San
Age: 18
Name:
Li Si
Age: 26
Name:
Wang Wu
Age: 34
Name:
Zhao Liu
Age: 40
-----------------------
Name:
Zhang San
Age: 18
Name:
Wang Wu
Age: 84
Name:
Zhao Liu
Age: 40