One , definition

Provides a way to access elements of an aggregate object in sequence , Without exposing the internal representation of the object .

Two , Code instance

*
demand

Write one yourself iterator iterator , ergodic list.

*
code implementation

Iterator Interface :
public interface Iterator { public Object first(); public Object next();
public boolean isDone(); public Object currentItem(); }
Specific iteration type interface :
public interface Aggregate { public Iterator createIterator(); }
Iterator implementation class :
public class ConcreteIterator implements Iterator { private ConcreteAggregate
aggregate; private int current = 0; public ConcreteIterator(ConcreteAggregate
aggregate) { this.aggregate = aggregate; } @Override public Object first() {
return aggregate.get(0); } @Override public Object next() { Object result =
null; current++; if (current < aggregate.count()) { result =
aggregate.get(current); } return result; } @Override public boolean isDone() {
return current >= aggregate.count() ? true : false; } @Override public Object
currentItem() { return aggregate.get(current); } }
list Iterator implementation class :
public class ConcreteAggregate implements Aggregate { private List<Object>
items = new ArrayList<>(); @Override public Iterator createIterator() { return
new ConcreteIterator(this); } public int count() { return items.size(); }
public Object get(int index) { return items.get(index); } public Object
add(Object value) { return items.add(value); } }
Test class :
public class Test { public static void main(String[] args) {
ConcreteAggregate aggregate = new ConcreteAggregate(); aggregate.add(" Zhang San ");
aggregate.add(" Li Si "); aggregate.add(" Wang Wu "); aggregate.add(" Zhao Liu ");
aggregate.add(" pseudo-ginseng "); Iterator iterator = new ConcreteIterator(aggregate); while
(!iterator.isDone()) { System.out.println(iterator.currentItem() + ", All fares ");
iterator.next(); } } }
 

Technology
©2019-2020 Toolsou All rights reserved,
java Four functional interfaces ( a key , simple )os Simple use of module Browser kernel ( understand ) Some East 14 Pay change 16 salary , Sincerity or routine ?HashMap Explain in detail It's unexpected Python Cherry tree (turtle The gorgeous style of Library )html Writing about cherry trees , Writing about cherry trees