-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterator.kt
More file actions
34 lines (25 loc) · 779 Bytes
/
Copy pathIterator.kt
File metadata and controls
34 lines (25 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package iterator
interface IteratorBehavior {
fun hasNext() : Boolean
fun next() : Any
fun reset()
}
class CollectionItem(
private var name: String,
private var description: String
) {
override fun toString(): String = "$name: $description"
}
class Iterator(private val items: ArrayList<CollectionItem>) : IteratorBehavior {
private var position = 0
override fun hasNext(): Boolean = (position < items.size)
override fun next(): Any = items[position++]
override fun reset() { position = 0 }
}
class Collection() {
constructor(_items: ArrayList<CollectionItem>) : this() {
items = _items
}
private var items = arrayListOf<CollectionItem>()
fun createIterator() : IteratorBehavior = Iterator(items)
}