(所有情况仅限于我写的程序中)
集合List分为ArrayList和LinkedList 他们本质上分别为数组和链表,他们都实现了Collection接口,重写add和size()方法。
我们现在定义了一个Iterator接口,然后我要求每一个容器都提供一个方法,返回给我一个实现了这个接口的对象,
实现了这个接口的类叫什么名并不重要,最关心的是它一定重写了hasNext()和Next()方法,具体怎么实现的跟具体的容器相关;
对于我们用户来说,我们只需要拿到它的Iterator,那个类叫什么名跟我们没有关系,
为什么要在Collection接口以及实现了Collection接口的类都设置有Iterator Iterator()方法,在ArrayList和LinkedList中内部类实现Iterator的接口,重写hasNext()和Next()方法呢?
主要是为了让每个接口的功能充分独立,都比较小,使用者往往只使用其中小部分的内容,只用到 其中一部分功能
Collection接口
package com.chouwu;
public interface Collection {
void add(Object o);
int size();
Iterator iterator();
}
Iterator接口
package com.chouwu;
public interface Iterator {
Object next();
boolean hasNext();
}
实现了Collection接口和包含是实现了Iterator接口的内部类的ArrayList类
package com.chouwu;
import com.chouwu.Collection;
public class ArrayList implements Collection {
private Object[] objects = new Object[10];
private int size;
public void add(Object o) {
if(size == objects.length) {
Object[] newObjects = new Object[2*objects.length];
System.arraycopy(objects, 0, newObjects, 0, objects.length);
objects = newObjects;
}
objects[size] = o;
size++;
}
public int size() {
return size;
}
@Override
public Iterator iterator() {
return new ArrayListIterator();
}
//内部类
private class ArrayListIterator implements Iterator {
int currentIndex = 0;
@Override
public Object next() {
Object o = objects[currentIndex];
currentIndex++;
return o;
}
@Override
public boolean hasNext() {
if(currentIndex >= size) return false;
return true;
}
}
}
实现了Collection接口和包含是实现了Iterator接口的内部类的LinkedList类
package com.chouwu;
import com.chouwu.Collection;
public class LinkedList implements Collection {
private Node head;
private Node tail;
private int size;
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();
}
//内部类
private class LinkedListIterator implements Iterator{
int currentIndex = 0;
Node node;
@Override
public Object next() {
if(currentIndex == 0){
node = head;
currentIndex ++;
return node.getO();
}
node = node.getNext();
return node.getO();
}
@Override
public boolean hasNext() {
if(node == tail) return false;
return true;
}
}
}
LinkedList的辅助类Node
package com.chouwu;
public class Node {
private Object o;
private Node next;
public Node(Object o, Node next) {
this.o = o;
this.next = next;
}
public Object getO() {
return o;
}
public void setO(Object o) {
this.o = o;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
辅助测试类Cat
package com.chouwu;
public class Cat {
private int id;
public Cat(int id) {
this.id = id;
}
@Override
public String toString() {
return "Cat [id=" + id + "]";
}
}
测试程序
package com.chouwu;
import com.chouwu.ArrayList;
import com.chouwu.LinkedList;
public class Test {
public static void main(String[] args) {
// ArrayList list = new ArrayList();
Collection c = new LinkedList();
for(int i = 0;i<1;i++) {
c.add(new Cat(i));
}
System.out.println(c.size());
Iterator it = c.iterator();
while(it.hasNext()) {
Object o = it.next();
System.out.println(o + " ");
}
}
}

本文介绍了ArrayList和LinkedList两种容器的实现方式,并深入探讨了如何通过内部类实现Iterator接口,以便为用户提供统一的迭代功能。文章还展示了具体的代码实现。
326

被折叠的 条评论
为什么被折叠?



