当前位置: 代码迷 >> Java相关 >> Java Iterator形式 模仿Collectin ArrayList LinckedList
  详细解决方案

Java Iterator形式 模仿Collectin ArrayList LinckedList

热度:21   发布时间:2016-04-22 19:59:57.0
Java Iterator模式 模仿Collectin ArrayList LinckedList

Java Iterator模式 Java Iterator模式, 模仿Collectin ArrayList LinckedList

一、有如下几个类

1.接口Collection.java

2.接口Iterator.java

3.ArrayList.java

4.LinkedList.java

5.Node.java

关系如下:

代码如下:

1.接口Collection.java

public interface Collection<E> {	public void add(E e);	public int size();	public Iterator iterator();}

  

2.ArrayList.java

public class ArrayList<E> implements Collection<E>{	//先给定一个长度为10的数组	Object [] objects = new Object[10];		//冗余一个int指数,方便判定是组是否为满和返回集合大小	int index = 0;	@Override	//1.动态添加元素	public void add(E e) {				//1.1先判断数组是否已满		if(index == objects.length){			Object [] newObjects = new Object[objects.length*2];			System.arraycopy(objects, 0, newObjects, 0, objects.length);			objects = newObjects;	//数组是引用数据类型		}				//1.2为新添加的元素指定下标		objects[index] = e;				//1.3index自加1,以方便返回集合在大小		index++;	}	//2.根据下标访问元素		@Override	//3.返回集合大小	public int size() {		return index;	}	@Override	public Iterator iterator() {		return new ArrayListIterator();	}		private class ArrayListIterator implements Iterator {		private int currentIndex = 0;				@Override		public Object next() {			// 返回最下一个元素			Object o = objects[currentIndex];			currentIndex++;			return o;		}		@Override		public boolean hasNext() {			// 判断是否为最后一个元素						if(currentIndex >= index){				return false;			}			return true;		}			}	}

  

3.LinkedList.java

public class LinkedList<E> implements Collection<E> {	private Node head;	private Node tail;	private int size;		public void add(E e){		Node n = new Node(e, null);		if(head == null){			head = n;			tail = n;			size++;		} else {			tail.setNext(n);			tail = n;			size++;		}	}		public int size(){		return size;	}	@Override	public Iterator iterator() {		return new LinkedListIterator();	}		private class LinkedListIterator implements Iterator {		private Node currentNode = head;				@Override		public Object next() {			Object o = currentNode.getData();			currentNode = currentNode.getNext();			return o;		}		@Override		public boolean hasNext() {			if(currentNode.getNext() == null){				return false;			}			return true;		}			}}

4.Node.java

public class Node {		private Object data;	private Node next;		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;	}		}

 

5.Iterator.java

public interface Iterator {	public Object next();	public boolean hasNext();}

 

6.Dog.java

public class Dog {	private int id;	public Dog(int id) {		super();		this.id = id;	}		@Override	public String toString() {		return "Dog"+id;	}}

7.测试类CollectionTest.java

public class CollectionTest {	@Test	public void test() {		Collection co = new LinkedList();		for(int i = 0 ;i < 15 ;i++){			co.add(new Dog(i));		}		System.out.println(co.size());				Iterator it = co.iterator();		while(it.hasNext()){			System.out.println(it.next());		}	}}

运行结果

 

  相关解决方案