Iterator 数组和链条

IMyCollection

package com.rain.Iterator;

public interface IMyCollection<E> {

	int size();
	
	void add(E e);
	
	IMyIterator<E> iterator();
	
}

IMyIterator

package com.rain.Iterator;

public interface IMyIterator <E>{

 boolean hasNext();
 E next();
 
}

MyArrayList

package com.rain.Iterator;

public class MyArrayList<E> implements IMyCollection<E> {

	Object[] oldO = new Object[10];// 容器
	int index = 0;// 下标

	@Override
	public int size() {
		return this.index;
	}

	@Override
	public void add(E e) {

		if (index >= oldO.length) {// 如果容器装满了
			Object[] newO = new Object[oldO.length * 2];// 创建 新容器扩容
			System.arraycopy(oldO, 0, newO, 0, oldO.length);// 将就容器的数据复制到新容器中
			oldO = newO;// 将引用指向新容器
		}
		oldO[index] = e;
		index++;
	}

	@Override
	public IMyIterator<E> iterator() {
		return new MyIterator();
	}
	
	// 内部类
	 class MyIterator implements IMyIterator<E> {

		int currentIndex = 0;

		@Override
		public boolean hasNext() {

			if (this.currentIndex < index)
				return true;
			else
				return false;
		}

		@Override
		public E next() {
			return (E) oldO[currentIndex++];
		}

	}


	
	
}

Node

package com.rain.Iterator;

public class Node {

	private Object data;
	private Node next;
	
	public Node(Object data, Node next) {
		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;
	}
	@Override
	public String toString() {
		return this.getData().toString();
	}
	public static void main(String[] args) {
		Node node=new Node("myNode", null);
		Node a=node;
		Node b=node;
		
		Node c=new Node("CCCC", null);
		b.setNext(c);//B有next了,现在看A会不会有next。因为AB指向的是同一块内存node
		
		System.out.println("A data:"+a.getData());
		System.out.println("B data:"+b.getData());
		
		System.out.println("A next data:"+a.getNext());
		System.out.println("B next data:"+b.getNext());
	}


}


MyLinkList

package com.rain.Iterator;

public class MyLinkList implements IMyCollection {

	int index=0;//下标
	Node head=null;//头结点
	Node tail=null;//尾结点
	
	@Override
	public int size() {
		return this.index;
	}

	@Override
	public void add(Object e) {
		
		Node node=new Node(e, null);
		
		if(this.head==null){
			head=node;
			tail=head;
			this.index++;
		}
		else{
			tail.setNext(node);
			tail=node;
			this.index++;
		}
	}

	@Override
	public IMyIterator iterator() {
		// TODO Auto-generated method stub
		return new MyIterator();
	}

class MyIterator implements IMyIterator{

	int currentIndex=0;
	
	@Override
	public boolean hasNext() {
		if(this.currentIndex<index)
			return true;
		else
			return false;
	}

	@Override
	public Object next() {

		if(this.currentIndex==0){
			this.currentIndex++;
			return head.getData();
		}
		else{
			Node next=head.getNext();
			
			for(int i=1;i<this.currentIndex;i++){
				next=next.getNext();
			}
			this.currentIndex++;
			return next.getData();
		}
			
	}
	
}

}




RainIterator 主程序

 

package com.rain.Iterator;

class People{
	public int id;
	public People(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return String.valueOf(id);
	}
}
public class	RainIterator {


	public static void main(String[] args) {
//		IMyCollection<People> collection=new MyArrayList<People>();
		IMyCollection<People> collection=new MyLinkList();
		
		
		for(int i=0;i<15;i++){
			collection.add(new People(i)); 
		}
		
		System.out.println(collection.size()+"个元素");
		
		IMyIterator<People> iterator=collection.iterator();
		
		
		while(iterator.hasNext()){
			System.out.print(iterator.next()+"	");
		}
		
	}

}



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值