zipWithNext所在位置是kotlin.collections.zipWithNext,其相关用法介绍如下。

用法一

fun <T> Iterable<T>.zipWithNext(): List<Pair<T, T>>

返回此集合中每两个相邻元素的对列表。

如果此集合包含的元素少于两个,则返回的列表为空。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val letters = ('a'..'f').toList()
val pairs = letters.zipWithNext()

println(letters) // [a, b, c, d, e, f]
println(pairs) // [(a, b), (b, c), (c, d), (d, e), (e, f)]
//sampleEnd
}

输出:

[a, b, c, d, e, f]
[(a, b), (b, c), (c, d), (d, e), (e, f)]

用法二

inline fun <T, R> Iterable<T>.zipWithNext(
    transform: (a: T, b: T) -> R
): List<R>

返回一个列表,其中包含将给定 transform 函数应用于此集合中的每对两个相邻元素的结果。

如果此集合包含的元素少于两个,则返回的列表为空。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val values = listOf(1, 4, 9, 16, 25, 36)
val deltas = values.zipWithNext { a, b -> b - a }

println(deltas) // [3, 5, 7, 9, 11]
//sampleEnd
}

输出:

[3, 5, 7, 9, 11]