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- 获取元素索引、当前累加器值和元素本身的函数,并计算下一个累加器值。