博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
迭代模式
阅读量:6954 次
发布时间:2019-06-27

本文共 2321 字,大约阅读时间需要 7 分钟。

hot3.png

package org.designPattern.iterator20;

public class Book {
    private String ISBN;
    private String name;
    private double price;
    
    public Book(String isbn, String name, double price) {
        ISBN = isbn;
        this.name = name;
        this.price = price;
    }
    public String getISBN() {
        return ISBN;
    }
    public void setISBN(String isbn) {
        ISBN = isbn;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    
    public void display() {
        System.out.println("ISBN=" + ISBN + ",name=" + name + ",price" + price);
    }
    
    
}

package org.designPattern.iterator20;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BookList {
    private List<Book> bookList;
    private int index;
    private Iterator iterator;
    
    public BookList() {
        bookList = new ArrayList<Book>();
    }
    
    //添加书籍
    public void addBook(Book book) {
        bookList.add(book);
    }
    
    //删除书籍
    public void deleteBook(Book book) {
        int bookIndex = bookList.indexOf(book);
        bookList.remove(bookIndex);
    }
    
//    //判断是否有下一本书
//    public boolean hasNext() {
//        if(index >= bookList.size()) {
//            return false;
//        }
//        return true;
//    }
//    
//    //获得下一本书
//    public Book getNext() {
//        return bookList.get(index++);
//    }
    
//    public List<Book> getBookList() {
//        return bookList;
//    }
    
    public Iterator Iterator() {
        return new Itr();
    }
    
    private class Itr implements Iterator{
        public boolean hasNext() {
            if(index >= bookList.size()) {
                return false;
            }
            return true;
        }
        public Object next() {
            return bookList.get(index++);
        }
        public void remove() {
            
        }
        
    }
    
}

package org.designPattern.iterator20;

import java.util.Iterator;
public class MainClss {
    public static void main(String[] args) {
        BookList bookList = new BookList();
        
        Book book1 = new Book("010203","Java编程思想",90);
        Book book2 = new Book("010204","Java从入门到精通",60);
        
        bookList.addBook(book1);
        bookList.addBook(book2);
        
//        while(bookList.hasNext()) {
//            Book book = bookList.getNext();
//            book.display();
//        }
        
//        List<Book> bookDateList = bookList.getBookList();
//        for(int i = 0; i < bookDateList.size(); i++) {
//            Book book = bookDateList.get(i);
//            book.display();
//        }
        
        Iterator iter = bookList.Iterator();
        while(iter.hasNext()) {
            Book book = (Book) iter.next();
            book.display();
        }
        
        
    }
}

转载于:https://my.oschina.net/goudingcheng/blog/538702

你可能感兴趣的文章
C语言中强制转换问题
查看>>
python--练习--for i in range(2,101)
查看>>
每天一个linux命令(25):chgrp命令
查看>>
【15】万魂杀服务器开发之原始NIO、Mina、Netty使用
查看>>
git rebase之前需要 commit 才行
查看>>
zabbix2.4.6升级zabbix3.0.8后无法发送报警邮件
查看>>
android -------- 打开本地浏览器或指定浏览器加载,打电话,打开第三方app
查看>>
fscanf、fprintf的返回值
查看>>
Notepad++ 快捷键
查看>>
当Win10软件窗口消失了怎么办
查看>>
我是怎么招聘程序员的
查看>>
shell逻辑判断、文件属性判断、if特殊用法、case判断
查看>>
Jenkins在Windows下的安装与配置
查看>>
数据结构与算法之递归和分治思想
查看>>
CentOS 6.2安装配置LAMP服务器(Apache+PHP5+MySQL)
查看>>
今天进行的将zzb从apache迁移到nginx
查看>>
PHP缓存
查看>>
CentOS6.5 webserver---网络配置
查看>>
java学习笔记(3)
查看>>
IOS UIView直接响应点击事件的解决方法
查看>>