Kotlin List.iterator() – Examples

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,

  1. Take a list of elements.
  2. Get iterator over the elements of this list using list.iterator().
  3. 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