下面是模拟java里面的ArrayList和LinkedList的迭代器的实现原理
(1)首先创建一个集合类Collection
package com.alan.iterator;
public interface Collection {
//添加元素
void add(Object o) ;
//得到长度
int size() ;
//得到迭代器
Iterator iterator() ;
}
(2)创建迭代器接口Iterator
package com.alan.iterator;
public interface Iterator {
//判断下一个有没有元素
boolean hasNext() ;
//得到下一个元素
Object next() ;
}
(3)创建ArrayList类
package com.alan.iterator;
import java.util.Arrays;
public class ArrayList implements Collection{
int DEFAULT_SIZE = 10 ;
Object[] elementData ;
int index ;
public ArrayList(){
elementData = new Object[DEFAULT_SIZE] ;
}
public ArrayList(int size){
elementData = new Object[size] ;
}
@Override
public void add(Object o) {
//检查数组是否已满
if(index>=elementData.length){
//扩容
ensureCapacity() ;
}
elementData[index] = o ;
index++ ;
}
//给数组扩容
public void ensureCapacity(){
Object[] newContainer = new Object[elementData.length*2] ;//扩容为原来的两倍
System.arraycopy(elementData, 0, newContainer, 0, elementData.length) ;
elementData = new Object[newContainer.length] ;
elementData = newContainer ;
}
@Override
public Iterator iterator() {
return new ArrayListIterator();
}
@Override
public int size() {
return 0;
}
private class ArrayListIterator implements Iterator{
int cousor =0 ;
@Override
public boolean hasNext() {
if(cousor <index){
return true ;
}
return false;
}
@Override
public Object next() {
Object o = elementData[cousor] ;
cousor++ ;
return o;
}
}
}
(5)创建LinkedList类
package com.alan.iterator;
public class LinkedList implements Collection{
private Node header ;
private Node next ;
private int index ;
public LinkedList(){
header = new Node(null) ;
}
@Override
public void add(Object o) {
// TODO Auto-generated method stub
if(next ==null){
next = new Node(o) ;
header.next = next ;
}else{
Node node = new Node(o) ;
next.next = node ;
next = node ;
}
index++ ;
}
@Override
public Iterator iterator() {
// TODO Auto-generated method stub
return new LinkedListIterator();
}
@Override
public int size() {
// TODO Auto-generated method stub
return index;
}
private class LinkedListIterator implements Iterator{
int cousor = 0 ;
Node n ;
@Override
public boolean hasNext() {
if(cousor <index){
return true ;
}
return false;
}
@Override
public Object next() {
if(cousor == 0){
n = header ;
}
n = n.next ;
cousor++ ;
return n.elementData;
}
}
private class Node{
Node next ;
Object elementData ;
public Node(Object elementData){
this.elementData = elementData ;
}
}
}
(6)创建测试类Client
package com.alan.iterator;
public class Client {
public static void main(String[] args) {
//模拟LinkedList
LinkedList list = new LinkedList() ;
for(int i=0;i<10;i++){
list.add(new Test(i)) ;
}
//模拟LinkedList的Iterator实现
Iterator it = list.iterator() ;
while(it.hasNext()){
Object o = it.next() ;
System.out.println("LinkedList:"+o);
}
//模拟ArrayList
ArrayList arrayList = new ArrayList() ;
for(int i=0;i<20;i++){
arrayList.add(new Test(i)) ;
}
//便利arrayList
Iterator arrayIt = arrayList.iterator() ;
while(arrayIt.hasNext()){
Object o = arrayIt.next() ;
System.out.println("ArrayList:"+o);
}
}
}
class Test{
int id ;
public Test(int id){
this.id = id ;
}
@Override
public String toString(){
return "obj:"+id+" " ;
}
}
输出的结果:
LinkedList:obj:0
LinkedList:obj:1
LinkedList:obj:2
LinkedList:obj:3
LinkedList:obj:4
LinkedList:obj:5
LinkedList:obj:6
LinkedList:obj:7
LinkedList:obj:8
LinkedList:obj:9
ArrayList:obj:0
ArrayList:obj:1
ArrayList:obj:2
ArrayList:obj:3
ArrayList:obj:4
ArrayList:obj:5
ArrayList:obj:6
ArrayList:obj:7
ArrayList:obj:8
ArrayList:obj:9
ArrayList:obj:10
ArrayList:obj:11
ArrayList:obj:12
ArrayList:obj:13
ArrayList:obj:14
ArrayList:obj:15
ArrayList:obj:16
ArrayList:obj:17
ArrayList:obj:18
ArrayList:obj:19