package com
public interface Collection {
void add(Object o);
int size();
Iterator iterator();
}
package com
public interface Iterator {
Object next();
boolean hasNext();
}
package com
import com
public class ArrayList implements Collection {
Object[] objects = new Object[
int index =
public void add(Object o) {
if(index == objects
Object[] newObjects = new Object[objects
System
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 =
@Override
public boolean hasNext() {
if(currentIndex >= index) return false;
else return true;
}
@Override
public Object next() {
Object o = objects[currentIndex];
currentIndex ++;
return o;
}
}
}
package com
import com
public class TestMain {
public static void main(String[] args) {
Collection c = new ArrayList();
for(int i=
c
}
System
Iterator it = c
while(it
Object o = it
System
}
}
}
運行結果
string
string
string
string
string
string
string
string
string
string
string
string
string
string
string
從以上可以看出
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20579.html