public interface Aggregate { // call the iterator Iterator interfaces implemented method of generating an instance of the class public abstract Iterator iterator (); }
public interface Iterator { // determines whether hasNext element method in the presence of and access method in a next element // Returns hasNext return to true boolean type, because when there is a next element, i.e. when there is no traverse to the end of the collection, return to false public abstract Boolean the hasNext (); // return type of Object, show that returns a set of elements, which further implies a next processing element to move the iterator. // implicit because the Iterator interface only know the name of the method, wondering what to do next in the end process, we need to look a class that implements the Iterator interface in order to understand the role of public abstract Object next (); }
public class Book { private final String name; public Book(String name) { this.name = name; } // get the book by getName method name, the name of the book is passed to the Book class external call the constructor to initialize Book Book class and the class as a parameter public String getName() { return name; } }
// set processing class, and implements the interface Aggregate iterator method his public class BookShelf the implements Aggregate { // definition of books field, he is an array of Book, the array size is specified at the time of generation of instance BookShelf // reason the private field is set to books is to prevent changing the external private Final Book [] books; private int Last = 0 ; public BookShelf(int maxsize) { this.books = new Book[maxsize]; } public Book getBookAt(int index) { return books[index]; } public void appendBooke(Book book) { this.books[last] = book; last++; } public int getLength() { return last; } @Override public Iterator iterator() { return new BookShelfIterator(this); } }
// Because of the need to play a role Iterator, it implements the interface public class BookShelfIterator the implements Iterator { // Bookshelf field indicates BookShelfIterator summer to be traversed, index field indicates the iterator is currently pointing book subscripts // constructor will be received examples BookShelf to Bookshelf stored in the field, and the index is initialized to 0 Private Final BookShelf Bookshelf; Private int index; public BookShelfIterator(BookShelf bookShelf) { this.bookShelf = bookShelf; this.index = 0; } @Override // hasNext Iterator method is a method declared in the interface, the method determines there is no next book, a long return ture, no returns false. // decided whether or not the next book, and the total index need only compare the number of copies of book shelves (bookShelf.getLength () return value) to determine public Boolean the hasNext () { return index < bookShelf.getLength (); } /** * * @return */ @Override // Next method returns the book (Example of Book) iterator is currently pointing. And let the iterator to the next book. First remove the book values of the variables do return, then let the index pointing to the back of a book public Object the Next () { Book book = bookShelf.getBookAt(index); index++; return book; } }
public class Main { public static void main (String [] args) { // bookShelf.iterator () obtained it applies to an Iterator traversal shelves, while the condition is it.hasNext portion (), as long as the books on the shelf will not stop. By it.hasNext program () books a traverse BookShelf Bookshelf = new new BookShelf (. 4 ); bookShelf.appendBooke(new Book("Around the World in 80 Days")); bookShelf.appendBooke(new Book("Bible")); bookShelf.appendBooke(new Book("Cinderella")); bookShelf.appendBooke(new Book("Daddy-Long-Legs")); Iterator it = bookShelf.iterator(); while (it.hasNext()) { Book book = (Book) it.next(); System.out.println(book.getName()); } } }