Class diagram
##
/** * 自定义集合接口, 类似java.util.Collection * 用于数据存储 * @author stone * */ public interface ICollection<T> { IIterator<T> iterator(); //返回迭代器 void add(T t); T get(int index); }
/** * 自定义迭代器接口 类似于java.util.Iterator * 用于遍历集合类ICollection的数据 * @author stone * */ public interface IIterator<T> { boolean hasNext(); boolean hasPrevious(); T next(); T previous(); }
/** * 集合类, 依赖于MyIterator * @author stone */ public class MyCollection<T> implements ICollection<T> { private T[] arys; private int index = -1; private int capacity = 5; public MyCollection() { this.arys = (T[]) new Object[capacity]; } @Override public IIterator<T> iterator() { return new MyIterator<T>(this); } @Override public void add(T t) { index++; if (index == capacity) { capacity *= 2; this.arys = Arrays.copyOf(arys, capacity); } this.arys[index] = t; } @Override public T get(int index) { return this.arys[index]; } }
/* * 迭代器(Iterator)模式 又叫做游标(Cursor)模式 * 提供一种方法访问一个容器(container)对象中各个元素,而又不需暴露该对象的内部细节。 * Iterator模式就是分离了集合对象的遍历行为,抽象出一个迭代器类来负责,这样既可以做到不暴露集合的内部结构,又可让外部代码透明的访问集合内部的数据 * * 若有新的存储结构,可new 一个ICollection, 对应的 new 一个IIterator来实现它的遍历 */ @SuppressWarnings({"rawtypes", "unchecked"}) public class Test { public static void main(String[] args) { ICollection<Integer> collection = new MyCollection<Integer>(); add(collection, 3, 5, 8, 12, 3, 3, 5); for (IIterator<Integer> iterator = collection.iterator(); iterator.hasNext();) { System.out.println(iterator.next()); } System.out.println("-------------"); ICollection collection2 = new MyCollection(); add(collection2, "a", "b", "c", 3, 8, 12, 3, 5); for (IIterator iterator = collection2.iterator(); iterator.hasNext();) { System.out.println(iterator.next()); } } static <T> void add(ICollection<T> c, T ...a) { for (T i : a) { c.add(i); } } }Print
3 5 8 12 3 3 5 ------------- a b c 3 8 12 3 5
The above is the detailed content of Code example of how to implement the Iterator pattern in Java. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要整理了Stream流的概念和使用的相关问题,包括了Stream流的概念、Stream流的获取、Stream流的常用方法等等内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.