|
val numbers = listOf("one", "two", "three", "four")
val numbersIterator = numbers.iterator()
while (numbersIterator.hasNext()) {
println(numbersIterator.next())
}
|
let numbers = listOf("one", "two", "three", "four")
let numbersIterator = numbers.iterator()
while (numbersIterator.hasNext()) {
print(numbersIterator.next())
}
|
val numbers = listOf("one", "two", "three", "four")
for (item in numbers) {
println(item)
}
|
let numbers = listOf("one", "two", "three", "four")
for (item in numbers) {
print(item)
}
|
val numbers = listOf("one", "two", "three", "four")
numbers.forEach {
println(it)
}
|
let numbers = listOf("one", "two", "three", "four")
numbers.forEach {
print(it)
}
|
List iterators
|
val numbers = listOf("one", "two", "three", "four")
val listIterator = numbers.listIterator()
while (listIterator.hasNext()) listIterator.next()
println("Iterating backwards:")
while (listIterator.hasPrevious()) {
print("Index: ${listIterator.previousIndex()}")
println(", value: ${listIterator.previous()}")
}
|
let numbers = listOf("one", "two", "three", "four")
let listIterator = numbers.listIterator()
while (listIterator.hasNext()) listIterator.next()
print("Iterating backwards:")
while (listIterator.hasPrevious()) {
print("Index: ${listIterator.previousIndex()}")
print(", value: ${listIterator.previous()}")
}
|
Mutable iterators
|
val numbers = mutableListOf("one", "two", "three", "four")
val mutableIterator = numbers.iterator()
mutableIterator.next()
mutableIterator.remove()
println("After removal: $numbers")
|
let numbers = mutableListOf("one", "two", "three", "four")
let mutableIterator = numbers.iterator()
mutableIterator.next()
mutableIterator.remove()
print("After removal: $numbers")
|
val numbers = mutableListOf("one", "four", "four")
val mutableListIterator = numbers.listIterator()
mutableListIterator.next()
mutableListIterator.add("two")
mutableListIterator.next()
mutableListIterator.set("three")
println(numbers)
|
let numbers = mutableListOf("one", "four", "four")
let mutableListIterator = numbers.listIterator()
mutableListIterator.next()
mutableListIterator.add("two")
mutableListIterator.next()
mutableListIterator.set("three")
print(numbers)
|