package cn.edu.hpu.iterator; public class ArrayList { //我们要用数组模拟一个可以添加任意大小元素的容器 //首先我们固定一个数组空间,再添加元素的时候,我们去判断 //这个空间是不是满了,如果满了再在此基础上再添加一个固定空间 //往下依次类推,这样就有了可以添加任意大小元素的容器 //当然,容器大小一定不可能超过计算机内存的大小 Object[] objects=new Object[10]; int index=0;//代表objects的下一个空的位置在哪里 //添加元素方法 public void add(Object o){ if(index==objects.length){ //原来的数组已满,要做扩展(原长度X2) Object[] newObjects=new Object[objects.length*2]; //把objects内容拷贝到新的数组newObjects上 System.arraycopy(objects, 0, newObjects, 0, objects.length);//数组拷贝函数 objects=newObjects;//之后把newObjects拷贝给objects数组 } objects[index]=o; index++; } //获取容器元素个数方法 public int size(){ return index; } }
测试:
package cn.edu.hpu.iterator; import cn.edu.hpu.iterator.ArrayList; public class ArrayListTest { public static void main(String[] args) { ArrayList al=new ArrayList(); for(int i=0;i<20;i++){ al.add(new Object()); } System.out.println(al.size()); } }测试结果:20
package cn.edu.hpu.iterator; 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; } }
package cn.edu.hpu.iterator; import cn.edu.hpu.iterator.ArrayList; public class ArrayListTest { public static void main(String[] args) { ArrayList al=new ArrayList(); for(int i=0;i<20;i++){ al.add(new Cat(i)); } System.out.println(al.size()); } }
package cn.edu.hpu.iterator; public class LinkedList { Node head=null;//头节点(以后的元素通过next得到) 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; } }
package cn.edu.hpu.iterator; 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; } }
package cn.edu.hpu.iterator; import cn.edu.hpu.iterator.LinkedList; public class LinkedListTest { public static void main(String[] args) { LinkedList ll=new LinkedList(); for(int i=0;i<20;i++){ ll.add(new Cat(i)); } System.out.println(ll.size()); } }测试结果:20
package cn.edu.hpu.iterator; public interface Collection { void add(Object o); int size(); }
package cn.edu.hpu.iterator; public class ArrayList implements Collection{ 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; } }
package cn.edu.hpu.iterator; public class LinkedList implements Collection{ 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; } }
package cn.edu.hpu.iterator; import cn.edu.hpu.iterator.ArrayList; import cn.edu.hpu.iterator.Collection; public class ArrayListTest { public static void main(String[] args) { Collection c=new ArrayList(); for(int i=0;i<20;i++){ c.add(new Cat(i)); } System.out.println(c.size()); ArrayList al=(ArrayList)c; for(int i=0;i<al.index;i++){ Cat cat=(Cat)al.objects[i]; System.out.print(cat.getId()+" "); } } }结果:
package cn.edu.hpu.iterator; public interface Iterator { Object next(); boolean hasNext(); }
package cn.edu.hpu.iterator; public interface Collection { void add(Object o); int size(); Iterator iterator(); }
public Iterator iterator(){ return null; }
package cn.edu.hpu.iterator; import cn.edu.hpu.iterator.ArrayList; import cn.edu.hpu.iterator.Collection; import cn.edu.hpu.iterator.Iterator; public class ArrayListTest { public static void main(String[] args) { Collection c=new ArrayList(); for(int i=0;i<20;i++){ c.add(new Cat(i)); } System.out.println(c.size()); /*之前的方法 ArrayList al=(ArrayList)c; for(int i=0;i<al.index;i++){ Cat cat=(Cat)al.objects[i]; System.out.print(cat.getId()+" "); }*/ //使用统一遍历器的遍历方法 ArrayList al=(ArrayList)c; Iterator it=c.iterator(); while(it.hasNext()){ Object o=it.next(); Cat cat=(Cat)o; System.out.print(cat.getId()+" "); } } }这样我们怎么改变Collection的实现类,都不影响遍历方式。
package cn.edu.hpu.iterator; public class ArrayList implements Collection{ 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(); } private class ArrayListIterator implements Iterator{ private int currentIndex=0; @Override public boolean hasNext() { if(currentIndex>index) return false; else return true; } @Override public Object next() { Object o=objects[currentIndex]; currentIndex++; return o; } } }
package cn.edu.hpu.iterator; import cn.edu.hpu.iterator.ArrayList; import cn.edu.hpu.iterator.Collection; import cn.edu.hpu.iterator.Iterator; public class ArrayListTest { public static void main(String[] args) { Collection c=new ArrayList(); for(int i=0;i<20;i++){ c.add(new Cat(i)); } System.out.println(c.size()); //使用统一遍历器的遍历方法 ArrayList al=(ArrayList)c; Iterator it=c.iterator(); while(it.hasNext()){ Object o=it.next(); Cat cat=(Cat)o; System.out.print(cat.getId()+" "); } } }测试结果: