list Iterator
Требования к платформе и версии: Common (1.0), JS (1.1), Native (1.3), Wasm-JS (1.8), Wasm-WASI (1.8)
expect open override fun listIterator(): MutableListIterator<E>
Возвращает итератор списка для элементов этого списка (в правильном порядке).
Если перебор списка нужно начать с определённого индекса, вместо использования этой функции и ручного перебора до нужного индекса можно использовать перегрузку listIterator, принимающую параметр Int.
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val list = listOf('a', 'b')
val iterator = list.listIterator()
// "Cursor" is at the beginning of the list,
// so there is no previous element, only a next one
println("iterator.hasPrevious() is ${iterator.hasPrevious()}") // false
println("iterator.hasNext() is ${iterator.hasNext()}") // true
// Let's scan the list in a forward direction
println(iterator.next()) // a
println(iterator.next()) // b
// Cursor is past the end of the list,
// so there is no next element, only a previous one
println("iterator.hasPrevious() is ${iterator.hasPrevious()}") // true
println("iterator.hasNext() is ${iterator.hasNext()}") // false
// Let's scan the list backwards, starting from the end
println(iterator.previous()) // b
println(iterator.previous()) // a
// We ran out of elements
// iterator.previous() // will fail with NoSuchElementException
// Empty list has an empty iterator
val emptyListIterator = emptyList<String>().listIterator()
println("emptyListIterator.hasNext() is ${emptyListIterator.hasNext()}") // false
println("emptyListIterator.hasPrevious() is ${emptyListIterator.hasPrevious()}") // false
//sampleEnd
}
expect open override fun listIterator(index: Int): MutableListIterator<E>
Возвращает итератор списка для элементов этого списка (в правильном порядке), начиная с указанного индекса.
Исключения
Примеры
import kotlin.math.*
import kotlin.test.*
fun main() {
//sampleStart
val list = listOf('a', 'b', 'c')
// The iterator will scan elements starting from 'c' (the element at the index = 2)
val sublistIterator = list.listIterator(index = 2)
// However, previous elements are also accessible
println("sublistIterator.hasPrevious() is ${sublistIterator.hasPrevious()}") // true
// One step forward
println(sublistIterator.next()) // c
// Two steps backward
println(sublistIterator.previous()) // c
println(sublistIterator.previous()) // b
// If the index is equal to the length of the list,
// the iterator's "cursor" will point past the last element and only previous elements
// will be accessible
val pastLastIterator = list.listIterator(index = 3)
println("pastLastIterator.hasPrevious() is ${pastLastIterator.hasPrevious()}") // true
println("pastLastIterator.hasNext() is ${pastLastIterator.hasNext()}") // false
println(pastLastIterator.previous()) // c
println(pastLastIterator.previous()) // b
// It's an error to use indices outside of list bounds
// list.listIterator(-1) // will fail with IndexOutOfBoundsException
// list.listIterator(list.size + 1) // will fail with IndexOutOfBoundsException
//sampleEnd
}
© 2010–2026 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-abstract-mutable-list/list-iterator.html