package com.study.iterator
import java.util.*/**
* 数组的集合
*/class ArrayList_ : Collection_ {privatevar elements = Array<Any>(10){}privatevar index =0fungetIndex()= index
fungetElements()= elements
overridefunadd(element: Any){if(index == elements.size){
elements = Arrays.copyOf(elements, elements.size *2)}
elements[index]= element
index++}overridefunsize(): Int = index
overridefungetIteartor_(): Iterator_ =ArrayListIterator(this)}
2.2 链表
package com.study.iterator
/**
* 链式存储
*/class LinkedList_ : Collection_ {privatevar head: Node?=nullprivatevar tail: Node?=nullprivatevar size =0/**
* 获取头部
*/fungetHead()= head
/**
* 获取尾部
*/fungetTail()= tail
/**
* 集合存储数据大小
*/fungetSize()= size
overridefunadd(element: Any){var n =Node(element)
n.next =nullif(head ==null){
head = n
tail = n
}
tail?.next = n
tail = n
size++}overridefunsize(): Int = size
overridefungetIteartor_(): Iterator_ =LinkedListIterator(this)/**
* o是当前节点存储的指
* next指向下一个节点
*/innerclassNode(var o: Any){var next: Node?=null}}
三、迭代器实现类
3.1 数组迭代器
package com.study.iterator
classArrayListIterator(val arrayList: ArrayList_): Iterator_ {/**
* 当前轮询的下标
*/var position =0overridefunhasNext(): Boolean = position < arrayList.getIndex()overridefunnext(): Any {var currentElement = arrayList.getElements()[position]
position++return currentElement
}}