Java Iterator mode imitates Collectin ArrayList LinckedList and arraylistiterator
Java Iterator mode, imitating Collectin ArrayList LinckedList
1. There are several types:
1. Interface Collection. java
2. Interface Iterator. java
3. ArrayList. java
4. Collections list. java
5. Node. java
The link is as follows:
The Code is as follows:
1. Interface 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> {// first, an array of 10 objects [] objects = new Object [10]; // redundant int indexes, it is convenient to determine whether the group is full or whether the returned set size int index = 0; @ Override // 1. add the public void add (E e) element dynamically {// 1.1 first determine whether the array is full if (index = objects. length) {Object [] newObjects = new Object [objects. length * 2]; System. arraycopy (objects, 0, newObjects, 0, objects. length); objects = newObjects; // The array is a reference data type} // 1.2 specify the subscript objects [index] = e for the newly added element; // 1.3index auto-increment 1, to facilitate the return of the Set in size index ++;} // 2. access the element @ Override // 3 according to the subscript. returns the public int size () {return index ;}@ Overridepublic Iterator iterator () {return new ArrayListIterator ();} private class ArrayListIterator implements Iterator {private int currentIndex = 0; @ Overridepublic Object next () {// returns the next Element Object o = objects [currentIndex]; currentIndex ++; return o ;}@ Overridepublic boolean hasNext () {// determine whether it is the last element if (currentIndex >=index) {return false ;}return true ;}}}
3. Listing list. 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;}@Overridepublic Iterator iterator() {return new LinkedListIterator();}private class LinkedListIterator implements Iterator {private Node currentNode = head;@Overridepublic Object next() {Object o = currentNode.getData();currentNode = currentNode.getNext();return o;}@Overridepublic 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;}@Overridepublic String toString() {return "Dog"+id;}}
7. Test class CollectionTest. java
public class CollectionTest {@Testpublic 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());}}}
Running result