scanIndexed所在位置是kotlin.collections.scanIndexed,其相關用法介紹如下。

用法一

inline fun <T, R> Array<out T>.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, T) -> R
): List<R>
inline fun <R> ByteArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Byte) -> R
): List<R>
inline fun <R> ShortArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Short) -> R
): List<R>
inline fun <R> IntArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Int) -> R
): List<R>
inline fun <R> LongArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Long) -> R
): List<R>
inline fun <R> FloatArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Float) -> R
): List<R>
inline fun <R> DoubleArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Double) -> R
): List<R>
inline fun <R> BooleanArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Boolean) -> R
): List<R>
inline fun <R> CharArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, Char) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UIntArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, UInt) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> ULongArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, ULong) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UByteArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, UByte) -> R
): List<R>
@ExperimentalUnsignedTypes inline fun <R> UShortArray.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, UShort) -> R
): List<R>

返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始數組中的索引和以 initial 值開頭的當前累加器值而生成的連續累加值。

請注意,傳遞給operation 函數的acc 值不應被改變;否則會影響結果列表中的前一個值。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scan("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.scanIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]

println(emptyList<String>().scan("s") { _, _ -> "X" }) // [s]
//sampleEnd
}

輸出:

[s, sa, sab, sabc, sabcd]
[s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]
[s]

參數

operation- 獲取元素索引、當前累加器值和元素本身的函數,並計算下一個累加器值。

用法二

inline fun <T, R> Iterable<T>.scanIndexed(
    initial: R, 
    operation: (index: Int, acc: R, T) -> R
): List<R>

返回一個列表,其中包含通過從左到右將 operation 應用於每個元素、其在原始集合中的索引和以 initial 值開頭的當前累加器值而生成的連續累加值。

請注意,傳遞給operation 函數的acc 值不應被改變;否則會影響結果列表中的前一個值。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val strings = listOf("a", "b", "c", "d")
println(strings.scan("s") { acc, string -> acc + string }) // [s, sa, sab, sabc, sabcd]
println(strings.scanIndexed("s") { index, acc, string -> acc + string + index }) // [s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]

println(emptyList<String>().scan("s") { _, _ -> "X" }) // [s]
//sampleEnd
}

輸出:

[s, sa, sab, sabc, sabcd]
[s, sa0, sa0b1, sa0b1c2, sa0b1c2d3]
[s]

參數

operation- 獲取元素索引、當前累加器值和元素本身的函數,並計算下一個累加器值。