Connection interface implementation:
package com.luna.model.iterator; /** * Connection interface implementation * @author luna */ public interface Connection { void add(Object o); int size(); Iterator iterator(); }
Iterator iterator interface implementation:
package com.luna.model.iterator; /** * Iterator Iterator implementation * @author luna */ public interface Iterator { Object next(); boolean hasNext(); }
ArrayList container class code implementation:
package com.luna.model.iterator; /** * ArrayList container class implementation * @author luna */ public class ArrayList implements Connection{ Object[] objects = new Object[10]; int index = 0; public void add(Object o){ if(index == objects.length){ Object[] newObjects = new Object[objects.length * 2]; System.arraycopy(objects, 0, newObjects, 0, objects.length); objects = newObjects; } objects[index] = o; index++; } public int size(){ return index; } public Iterator iterator(){ return new ArrayListIterator(); } public 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; else return true; } } }
LinkedList node Node implementation:
package com.luna.model.iterator; /** * Node node implementation * @author luna */ 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; } }
LinkedList container implementation class:
package com.luna.model.iterator; /** * LinkedList container implementation * @author luna */ public class LinkedList implements Connection{ Node head = null; Node tail = null; int size = 0; public void add(Object o){ Node n = new Node (o, null); if(head == null){ head = n; tail = n; } tail.setNext(n); tail = n; size ++; } public int size(){ return size; } @Override public Iterator iterator() { return new LinkedListIterator(); } public class LinkedListIterator implements Iterator{ @Override public Object next() { Object o = tail.getData(); return o; } @Override public boolean hasNext() { if(tail == null) return false; else return true; } } }
Test entity class Cat:
package com.luna.model.iterator; /** * Test entity object class cat * @author luna */ public class Cat { private int id; public Cat(int id) { super(); this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "cat:" + id; } }
Container test class:
package com.luna.model.iterator; public class Test { public static void main(String[] args) { // ArrayList a = new ArrayList(); // LinkedList a = new LinkedList(); Connection a = new ArrayList(); for (int i = 0; i < 15; i++) { a.add(new Cat(i)); } System.out.println("The size of ArrayList is: "+a.size()); Iterator it = a.iterator(); while(it.hasNext()){ Object o = it.next(); System.out.print(o+" "); } } }