标签:
迭代子模式又叫游标(Cursor)模式,是对象的行为模式。迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象 --阎宏博士的《JAVA与模式》
集合接口
public interface MyCollection { void add(Object o); int size(); }
数组集合
public class MyArrayList implements MyCollection { private Object[] objects = new Object[10]; // 初始容器大小 private int position = 0; // 游标(下一个插入元素的位置) @Override public void add(Object o) { if(position == objects.length) { // 如果容器已满,扩充容器 Object[] newObjects = new Object[objects.length * 2]; System.arraycopy(objects, 0, newObjects, 0, objects.length); objects = newObjects; } objects[position] = o; position ++; } @Override public int size() { return position; } }
链表集合
public class MyLinkedList implements MyCollection { private Node head = null; private Node tail = null; private int size = 0; @Override public void add(Object o) { Node node = new Node(o, null); if(head == null) { // 第一个 head = node; tail = node; } else { tail.setNext(node); tail = node; } size ++; } @Override public int size() { return size; } 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; } } }
OK,我们发现MyArrayList和MyLinkedList虽都实现了MyCollection接口,但具体实现方法不同,导致遍历方式也不同,要想统一遍历方式只能用一种抽象类/接口的方式
第二版:统一遍历方法
增加统一的遍历接口
public interface MyIterator { Object next(); boolean hasNext(); }
让所有集合类都要实现该接口(只要实现了MyCollection接口的集合类我们就可以调用其iterator()方法遍历其元素)
public interface MyCollection { void add(Object o); int size(); MyIterator iterator(); }
此时再遍历集合就很简单了
while(iterator.hasNex()) { Object data = iterator.next(); }
下面开始实现MyIterator - 数组集合
public class MyArrayList implements MyCollection { private Object[] objects = new Object[10]; // 初始容器大小 private int position = 0; // 游标(下一个插入元素的位置) @Override public void add(Object o) { if(position == objects.length) { // 如果容器已满,扩充容器 Object[] newObjects = new Object[objects.length * 2]; System.arraycopy(objects, 0, newObjects, 0, objects.length); objects = newObjects; } objects[position] = o; position ++; } @Override public int size() { return position; } @Override public MyIterator iterator() { return new MyIterator() { int index = 0; @Override public Object next() { return objects[index ++]; } @Override public boolean hasNext() { // if(index < objects.length) // 低级错误 if(index < position) return true; return false; } }; } }
链表集合
public class MyLinkedList implements MyCollection { private Node head = null; private Node tail = null; private int size = 0; @Override public void add(Object o) { Node node = new Node(o, null); if(head == null) { // 第一个 head = node; tail = node; } else { tail.setNext(node); tail = node; } size ++; } @Override public int size() { return size; } 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 MyIterator iterator() { return new MyIterator() { Node currNode = head; @Override public Object next() { Object o = currNode.getData(); currNode = currNode.getNext(); return o; } @Override public boolean hasNext() { return currNode != null; } }; } }
测试
public class Test { public static void main(String[] args) { MyCollection al = new MyArrayList(); MyCollection ll = new MyLinkedList(); for(int i=0; i<15; i++) { al.add(i); ll.add(i); } MyIterator iterator = ll.iterator(); // = al.iterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }
Ok,以上就是山寨乞丐版JDK ArrayList 和 LinkedList
标签:
原文地址:http://www.cnblogs.com/wangj1130/p/4822923.html