package
com.zj.iterator;
public
interface
MyIterator<T> {
boolean
end();
T current();
void
next();
}
|
package
com.zj.iterator;
import
java.util.ArrayList;
import
java.util.List;
public
class
Links<T> {
private
List<T>
items
=
new
ArrayList<T>();
public
void
add(T x) {
items
.add(x);
}
private
class
LinksIterator
implements
MyIterator<T> {
private
int
i
= 0;
public
boolean
end() {
return
i
==
items
.size();
}
public
T current() {
return
items
.get(
i
);
}
public
void
next() {
if
(
i
<
items
.size())
i
++;
}
}
public
MyIterator<T> browser() {
return
new
LinksIterator();
}
public
static
void
main(String[] args) {
Links<Integer> links =
new
Links<Integer>();
for
(
int
i = 1; i < 6; i++)
links.add(i);
// use MyIterator
MyIterator<Integer> myItr = links.browser();
while
(!myItr.end()) {
System.
out
.print(myItr.current() +
" "
);
myItr.next();
}
}
}
|
package
com.zj.iterator;
import
java.util.ArrayList;
import
java.util.List;
public
class
Links2<T> {
private
List<T>
items
=
new
ArrayList<T>();
public
void
add(T x) {
items
.add(x);
}
public
MyIterator<T> browser() {
return
new
MyIterator<T>() {
private
int
i
= 0;
public
boolean
end() {
return
i
==
items
.size();
}
public
T current() {
return
items
.get(
i
);
}
public
void
next() {
if
(
i
<
items
.size())
i
++;
}
};
}
public
static
void
main(String[] args) {
Links2<Integer> links =
new
Links2<Integer>();
for
(
int
i = 1; i < 6; i++)
links.add(i);
// use MyIterator
MyIterator<Integer> myItr = links.browser();
while
(!myItr.end()) {
System.
out
.print(myItr.current() +
" "
);
myItr.next();
}
}
}
|
package
java.util;
public
interface
Iterator<E> {
boolean
hasNext();
E next();
void
remove();
}
|
package
com.zj.iterator.standard;
import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.List;
public
class
Links3<T> {
private
List<T>
items
=
new
ArrayList<T>();
public
void
add(T x) {
items
.add(x);
}
public
Iterator<T> iterator() {
return
new
Iterator<T>() {
private
int
index
= 0;
public
boolean
hasNext() {
return
index
<
items
.size();
}
public
T next() {
return
items
.get(
index
++);
}
public
void
remove() {
// Not implemented
throw
new
UnsupportedOperationException();
}
};
}
public
static
void
main(String[] args) {
Links3<Integer> links =
new
Links3<Integer>();
for
(
int
i = 1; i < 6; i++)
links.add(i);
// use Standard Iterator
Iterator<Integer> myItr = links.iterator();
while
(myItr.hasNext())
System.
out
.print(myItr.next() +
" "
);
}
}
|
package
java.lang;
import
java.util.Iterator;
public
interface
Iterable<T> {
Iterator<T> iterator();
}
|
package
com.zj.iterator.standard;
import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.List;
public
class
Links4<T>
implements
Iterable<T> {
private
List<T>
items
=
new
ArrayList<T>();
public
void
add(T x) {
items
.add(x);
}
public
Iterator<T> iterator() {
return
new
Iterator<T>() {
private
int
index
= 0;
public
boolean
hasNext() {
return
index
<
items
.size();
}
public
T next() {
return
items
.get(
index
++);
}
public
void
remove() {
// Not implemented
throw
new
UnsupportedOperationException();
}
};
}
public
static
void
main(String[] args) {
Links4<Integer> links =
new
Links4<Integer>();
for
(
int
i = 1; i < 6; i++)
links.add(i);
// use Foreach
for
(Integer integer : links)
System.
out
.print(integer +
" "
);
}
}
|
package
com.zj.iterator.standard;
import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.List;
public
class
Links5<T>
implements
Iterable<T> {
private
List<T>
items
=
new
ArrayList<T>();
public
void
add(T x) {
items
.add(x);
}
public
Iterator<T> iterator() {
return
new
Iterator<T>() {
private
int
index
= 0;
public
boolean
hasNext() {
return
index
<
items
.size();
}
public
T next() {
return
items
.get(
index
++);
}
public
void
remove() {
// Not implemented
throw
new
UnsupportedOperationException();
}
};
}
public
Iterable<T> even() {
return
new
Iterable<T>() {
public
Iterator<T> iterator() {
return
new
Iterator<T>() {
private
int
index
= 0;
public
boolean
hasNext() {
return
index
<
items
.size();
}
public
T next() {
index
+= 2;
return
items
.get(
index
- 2);
}
public
void
remove() {
// Not implemented
throw
new
UnsupportedOperationException();
}
};
}
};
}
public
static
void
main(String[] args) {
Links5<Integer> links =
new
Links5<Integer>();
for
(
int
i = 1; i < 6; i++)
links.add(i);
// use Foreach default
for
(Integer integer : links)
System.
out
.print(integer +
" "
);
System.
out
.println();
// use Foreach even
for
(Integer integer : links.even())
System.
out
.print(integer +
" "
);
}
}
|