Iterator-迭代器

    • package com.yjwang.Iterator;
    • public interface Aggregate {
    •     public abstract Iterator iterator();
    • }
  1. package com.yjwang.Iterator;
  2. public interface Iterator {
  3.     public abstract boolean hasNext();
  4.     public abstract Object Next();
  5. }
  1. package com.yjwang.Iterator;
  2. public class Book {    
  3.     private String name;
  4.     
  5.     public Book(String name){
  6.         this.name = name;
  7.     }
  8.     
  9.     public String getName(){
  10.         return name;
  11.     }
  12.     public void setName(String name) {
  13.         this.name = name;
  14.     }
  15. }
  1. package com.yjwang.Iterator;
  2. import java.util.Vector;
  3. public class BookShelf implements Aggregate{
  4.     //private Book[] books;
  5.     private Vector books;
  6.     private int last = 0;
  7.     
  8.     /**定义书架的长度***/
  9.     public BookShelf(int maxsize) {
  10.         //this.books = new Book[maxsize];
  11.         this.books = new Vector(maxsize);
  12.     }
  13.     
  14.     public Book getBookAt(int index){
  15.         //return books[index];
  16.         return (Book)books.get(index);
  17.     }
  18.     
  19.     public void appendBook(Book book){
  20.         //this.books[last] = book;
  21.         books.add(book);
  22.         //last++;
  23.     }
  24.     
  25.     /**统计书的总数**/
  26.     public int getLength(){
  27.         //return last;
  28.         return books.size();
  29.     }
  30.     
  31.     public Iterator iterator(){
  32.         return new BookShelfIterator(this);
  33.     }
  34. }
  1. package com.yjwang.Iterator;
  2. public class BookShelfIterator implements Iterator {
  3.     private BookShelf bookShelf;
  4.     private int index;
  5.     
  6.     public BookShelfIterator(BookShelf bookShelf){
  7.         this.bookShelf = bookShelf;
  8.         this.index = 0;
  9.     }
  10.     
  11.     public boolean hasNext() {
  12.         // TODO Auto-generated method stub
  13.         if(index < bookShelf.getLength()){
  14.             return true;
  15.         }
  16.         else{
  17.         return false;
  18.         }
  19.     }
  20.     public Object Next() {
  21.         // TODO Auto-generated method stub
  22.         Book book = bookShelf.getBookAt(index);
  23.         index++;
  24.         return book;
  25.     }
  26.     
  27. }
  1. package com.yjwang.Iterator;
  2. public class MainTest {
  3.     public static void main(String args[]){
  4.         BookShelf bookShelf = new BookShelf(4);
  5.         bookShelf.appendBook(new Book("A_book1"));
  6.         bookShelf.appendBook(new Book("B_book2"));
  7.         bookShelf.appendBook(new Book("C_book3"));
  8.         bookShelf.appendBook(new Book("D_book4"));
  9.         bookShelf.appendBook(new Book("E_book5"));
  10.         Iterator it = bookShelf.iterator();
  11.         while(it.hasNext()){
  12.             Book book = (Book)it.Next();
  13.             System.out.println(" " + book.getName());
  14.         }
  15.     }
  16. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值