设计模式-Iterator理解

为了更好的理解Iterator,也就是迭代器,借用ArrayList和LinkedList,来更好解释了Iterator的作用

package designMode.iterator;
/**
 * 
 * @Description 定义集合方法接口
 * @author CCQ
 * @date 2017年6月20日 下午9:52:30
 *
 */
public interface Collection {
	
	void add(Object o);
	
	int size();
	
	Iterator iterator();

}


package designMode.iterator;
/**
 * 
 * @Description 用来遍历接口
 * @author CCQ
 * @date 2017年6月20日 下午9:53:03
 *
 */
public interface Iterator {
	boolean hasNext();
	Object next();
}


package designMode.iterator;
/**
 * 
 * @Description 模拟ArrayList实现
 * @author CCQ
 * @date 2017年6月20日 下午8:57:15
 *
 */
public class ArrayList implements Collection {
	
	Object[] objects = new Object[10];
	int index = 0; //记录当前加入的对象数目
	public void add(Object o){
		//判断数组长度是否够用
		if(objects.length == index){
			Object[] newObjects = new Object[objects.length*3/2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = o;
		index++;
	}
	/**
	 * 返回list的长度
	 */
	public int size(){
		return index;
	}
	/**
	 * 提供iterator方法,进行遍历
	 */
	public Iterator iterator(){
		return new arrayListIterator();
	}
	//内部类,实现Iterator接口,提供hasNext和next方法
	public class arrayListIterator implements Iterator{
		
		int currentIndex = 0;
		@Override
		public boolean hasNext() {
			if(currentIndex > index - 1) 
				return false;
			return true;
		}

		@Override
		public Object next() {
			Object o = objects[currentIndex];
			currentIndex++;
			return o;
		}
	}
}


package designMode.iterator;
/**
 * 
 * @Description 定义一个结点
 * @author CCQ
 * @date 2017年6月20日 下午9:54:54
 *
 */
public class Node {
	
	public Node(Object data, Node next) {
		super();
		this.data = data;
		this.next = next;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public Node getNext() {
		return next;
	}
	public void setNext(Node next) {
		this.next = next;
	}
	private Object data;
	private Node next;

}


package designMode.iterator;
/**
 * 
 * @Description 
 * @author CCQ
 * @date 2017年6月20日 下午8:57:08
 *
 */
public class LinkedList implements Collection {
	
	Node head = null; //头指针
	Node tail = null; //尾指针
	int size = 0;     //链表的长度
	//添加一个对象
	public void add(Object o){
		Node node = new Node(o, null);
		
		if(head == null){
			head = node;
			tail = node;
		}
		tail.setNext(node);
		tail = node;
		size++;
		
	}
	
	public int size(){
		return size;
	}

	@Override
	public Iterator iterator() {
		
		return new linkedListIterator();
	}
	//内部类,实现Iterator
	public class linkedListIterator implements Iterator{
		
		Node currentNode = head;
		int currentIndex = 1;
		
		@Override
		public boolean hasNext() {
			if(currentIndex > size)
				return false;
			else
				return true;
		}

		@Override
		public Object next() {
			Object o = currentNode.getData();
			currentNode = currentNode.getNext();
			currentIndex++;
			return o;
		}
		
	}

}

package designMode.iterator;
/**
 * 
 * @Description 测试类
 * @author CCQ
 * @date 2017年6月20日 下午9:55:11
 *
 */
public class Test {
	
	public static void main(String[] args) {
		Collection list = new ArrayList();
		for(int i=0;i<15;i++){
			list.add(i);
		}
		System.out.println("ArrayList长度:"+list.size());
		//使用Iterator遍历
		Iterator iterator = list.iterator();
		while(iterator.hasNext()){
			System.out.print(iterator.next()+" ");
		}
		System.out.println();
		
		
		Collection list2 = new LinkedList();
		for (int i = 0; i < 15; i++) {
			list2.add(i);
		}
		System.out.println("LinkedList长度:"+list2.size());
		//使用Iterator遍历
		Iterator iterator2 = list2.iterator();
		while(iterator2.hasNext()){
			System.out.print(iterator2.next()+" ");
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值