Kotlin List.iterator()
The Kotlin List.iterator() function returns an iterator over the elements of this list.
Syntax
List.iterator()
Example 1
In this example,
- Take a list of elements.
- Get iterator over the elements of this list using list.iterator().
- Use this iterator to iterate over the elements and print them.
Program
fun main(args: Array<String>) {
val list1 = listOf("a", "b", "c", "d")
val list1Iterator = list1.iterator()
while( list1Iterator.hasNext() ) {
println(list1Iterator.next())
}
}
Output
a
b
c
d