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

用法一

infix fun <T, R> Array<out T>.zip(
    other: Array<out R>
): List<Pair<T, R>>
infix fun <R> ByteArray.zip(
    other: Array<out R>
): List<Pair<Byte, R>>
infix fun <R> ShortArray.zip(
    other: Array<out R>
): List<Pair<Short, R>>
infix fun <R> IntArray.zip(
    other: Array<out R>
): List<Pair<Int, R>>
infix fun <R> LongArray.zip(
    other: Array<out R>
): List<Pair<Long, R>>
infix fun <R> FloatArray.zip(
    other: Array<out R>
): List<Pair<Float, R>>
infix fun <R> DoubleArray.zip(
    other: Array<out R>
): List<Pair<Double, R>>
infix fun <R> BooleanArray.zip(
    other: Array<out R>
): List<Pair<Boolean, R>>
infix fun <R> CharArray.zip(
    other: Array<out R>
): List<Pair<Char, R>>
infix fun ByteArray.zip(
    other: ByteArray
): List<Pair<Byte, Byte>>
infix fun ShortArray.zip(
    other: ShortArray
): List<Pair<Short, Short>>
infix fun IntArray.zip(other: IntArray): List<Pair<Int, Int>>
infix fun LongArray.zip(
    other: LongArray
): List<Pair<Long, Long>>
infix fun FloatArray.zip(
    other: FloatArray
): List<Pair<Float, Float>>
infix fun DoubleArray.zip(
    other: DoubleArray
): List<Pair<Double, Double>>
infix fun BooleanArray.zip(
    other: BooleanArray
): List<Pair<Boolean, Boolean>>
infix fun CharArray.zip(
    other: CharArray
): List<Pair<Char, Char>>
@ExperimentalUnsignedTypes infix fun <R> UIntArray.zip(
    other: Array<out R>
): List<Pair<UInt, R>>
@ExperimentalUnsignedTypes infix fun <R> ULongArray.zip(
    other: Array<out R>
): List<Pair<ULong, R>>
@ExperimentalUnsignedTypes infix fun <R> UByteArray.zip(
    other: Array<out R>
): List<Pair<UByte, R>>
@ExperimentalUnsignedTypes infix fun <R> UShortArray.zip(
    other: Array<out R>
): List<Pair<UShort, R>>
@ExperimentalUnsignedTypes infix fun UIntArray.zip(
    other: UIntArray
): List<Pair<UInt, UInt>>
@ExperimentalUnsignedTypes infix fun ULongArray.zip(
    other: ULongArray
): List<Pair<ULong, ULong>>
@ExperimentalUnsignedTypes infix fun UByteArray.zip(
    other: UByteArray
): List<Pair<UByte, UByte>>
@ExperimentalUnsignedTypes infix fun UShortArray.zip(
    other: UShortArray
): List<Pair<UShort, UShort>>

返回由具有相同索引的this 数组和other 数组的元素构建的对列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
println(listA zip listB) // [(a, 1), (b, 2), (c, 3)]
//sampleEnd
}

输出:

[(a, 1), (b, 2), (c, 3)]

用法二

inline fun <T, R, V> Array<out T>.zip(
    other: Array<out R>, 
    transform: (a: T, b: R) -> V
): List<V>
inline fun <R, V> ByteArray.zip(
    other: Array<out R>, 
    transform: (a: Byte, b: R) -> V
): List<V>
inline fun <R, V> ShortArray.zip(
    other: Array<out R>, 
    transform: (a: Short, b: R) -> V
): List<V>
inline fun <R, V> IntArray.zip(
    other: Array<out R>, 
    transform: (a: Int, b: R) -> V
): List<V>
inline fun <R, V> LongArray.zip(
    other: Array<out R>, 
    transform: (a: Long, b: R) -> V
): List<V>
inline fun <R, V> FloatArray.zip(
    other: Array<out R>, 
    transform: (a: Float, b: R) -> V
): List<V>
inline fun <R, V> DoubleArray.zip(
    other: Array<out R>, 
    transform: (a: Double, b: R) -> V
): List<V>
inline fun <R, V> BooleanArray.zip(
    other: Array<out R>, 
    transform: (a: Boolean, b: R) -> V
): List<V>
inline fun <R, V> CharArray.zip(
    other: Array<out R>, 
    transform: (a: Char, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> UIntArray.zip(
    other: Array<out R>, 
    transform: (a: UInt, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> ULongArray.zip(
    other: Array<out R>, 
    transform: (a: ULong, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> UByteArray.zip(
    other: Array<out R>, 
    transform: (a: UByte, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> UShortArray.zip(
    other: Array<out R>, 
    transform: (a: UShort, b: R) -> V
): List<V>

使用提供的应用于每对元素的 transform 函数,返回从 this 数组和 other 数组的元素构建的值列表,该数组具有相同的索引。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
val result = listA.zip(listB) { a, b -> "$a$b" }
println(result) // [a1, b2, c3]
//sampleEnd
}

输出:

[a1, b2, c3]

用法三

infix fun <T, R> Array<out T>.zip(
    other: Iterable<R>
): List<Pair<T, R>>
infix fun <R> ByteArray.zip(
    other: Iterable<R>
): List<Pair<Byte, R>>
infix fun <R> ShortArray.zip(
    other: Iterable<R>
): List<Pair<Short, R>>
infix fun <R> IntArray.zip(
    other: Iterable<R>
): List<Pair<Int, R>>
infix fun <R> LongArray.zip(
    other: Iterable<R>
): List<Pair<Long, R>>
infix fun <R> FloatArray.zip(
    other: Iterable<R>
): List<Pair<Float, R>>
infix fun <R> DoubleArray.zip(
    other: Iterable<R>
): List<Pair<Double, R>>
infix fun <R> BooleanArray.zip(
    other: Iterable<R>
): List<Pair<Boolean, R>>
infix fun <R> CharArray.zip(
    other: Iterable<R>
): List<Pair<Char, R>>
@ExperimentalUnsignedTypes infix fun <R> UIntArray.zip(
    other: Iterable<R>
): List<Pair<UInt, R>>
@ExperimentalUnsignedTypes infix fun <R> ULongArray.zip(
    other: Iterable<R>
): List<Pair<ULong, R>>
@ExperimentalUnsignedTypes infix fun <R> UByteArray.zip(
    other: Iterable<R>
): List<Pair<UByte, R>>
@ExperimentalUnsignedTypes infix fun <R> UShortArray.zip(
    other: Iterable<R>
): List<Pair<UShort, R>>

返回由具有相同索引的this 集合和other 数组的元素构建的对列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
println(listA zip listB) // [(a, 1), (b, 2), (c, 3)]
//sampleEnd
}

输出:

[(a, 1), (b, 2), (c, 3)]

用法四

inline fun <T, R, V> Array<out T>.zip(
    other: Iterable<R>, 
    transform: (a: T, b: R) -> V
): List<V>
inline fun <R, V> ByteArray.zip(
    other: Iterable<R>, 
    transform: (a: Byte, b: R) -> V
): List<V>
inline fun <R, V> ShortArray.zip(
    other: Iterable<R>, 
    transform: (a: Short, b: R) -> V
): List<V>
inline fun <R, V> IntArray.zip(
    other: Iterable<R>, 
    transform: (a: Int, b: R) -> V
): List<V>
inline fun <R, V> LongArray.zip(
    other: Iterable<R>, 
    transform: (a: Long, b: R) -> V
): List<V>
inline fun <R, V> FloatArray.zip(
    other: Iterable<R>, 
    transform: (a: Float, b: R) -> V
): List<V>
inline fun <R, V> DoubleArray.zip(
    other: Iterable<R>, 
    transform: (a: Double, b: R) -> V
): List<V>
inline fun <R, V> BooleanArray.zip(
    other: Iterable<R>, 
    transform: (a: Boolean, b: R) -> V
): List<V>
inline fun <R, V> CharArray.zip(
    other: Iterable<R>, 
    transform: (a: Char, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> UIntArray.zip(
    other: Iterable<R>, 
    transform: (a: UInt, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> ULongArray.zip(
    other: Iterable<R>, 
    transform: (a: ULong, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> UByteArray.zip(
    other: Iterable<R>, 
    transform: (a: UByte, b: R) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <R, V> UShortArray.zip(
    other: Iterable<R>, 
    transform: (a: UShort, b: R) -> V
): List<V>

使用提供的应用于每对元素的 transform 函数,返回从 this 数组和具有相同索引的 other 集合的元素构建的值列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
val result = listA.zip(listB) { a, b -> "$a$b" }
println(result) // [a1, b2, c3]
//sampleEnd
}

输出:

[a1, b2, c3]

用法五

inline fun <V> ByteArray.zip(
    other: ByteArray, 
    transform: (a: Byte, b: Byte) -> V
): List<V>
inline fun <V> ShortArray.zip(
    other: ShortArray, 
    transform: (a: Short, b: Short) -> V
): List<V>
inline fun <V> IntArray.zip(
    other: IntArray, 
    transform: (a: Int, b: Int) -> V
): List<V>
inline fun <V> LongArray.zip(
    other: LongArray, 
    transform: (a: Long, b: Long) -> V
): List<V>
inline fun <V> FloatArray.zip(
    other: FloatArray, 
    transform: (a: Float, b: Float) -> V
): List<V>
inline fun <V> DoubleArray.zip(
    other: DoubleArray, 
    transform: (a: Double, b: Double) -> V
): List<V>
inline fun <V> BooleanArray.zip(
    other: BooleanArray, 
    transform: (a: Boolean, b: Boolean) -> V
): List<V>
inline fun <V> CharArray.zip(
    other: CharArray, 
    transform: (a: Char, b: Char) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <V> UIntArray.zip(
    other: UIntArray, 
    transform: (a: UInt, b: UInt) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <V> ULongArray.zip(
    other: ULongArray, 
    transform: (a: ULong, b: ULong) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <V> UByteArray.zip(
    other: UByteArray, 
    transform: (a: UByte, b: UByte) -> V
): List<V>
@ExperimentalUnsignedTypes inline fun <V> UShortArray.zip(
    other: UShortArray, 
    transform: (a: UShort, b: UShort) -> V
): List<V>

使用提供的应用于每对元素的 transform 函数,返回从 this 数组和 other 数组的元素构建的值列表,该数组具有相同的索引。返回的列表具有最短数组的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
val result = listA.zip(listB) { a, b -> "$a$b" }
println(result) // [a1, b2, c3]
//sampleEnd
}

输出:

[a1, b2, c3]

用法六

infix fun <T, R> Iterable<T>.zip(
    other: Array<out R>
): List<Pair<T, R>>

返回从 this 集合的元素和具有相同索引的 other 数组的元素构建的对列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
println(listA zip listB) // [(a, 1), (b, 2), (c, 3)]
//sampleEnd
}

输出:

[(a, 1), (b, 2), (c, 3)]

用法七

inline fun <T, R, V> Iterable<T>.zip(
    other: Array<out R>, 
    transform: (a: T, b: R) -> V
): List<V>

使用提供的应用于每对元素的 transform 函数,返回从 this 集合和具有相同索引的 other 数组的元素构建的值列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
val result = listA.zip(listB) { a, b -> "$a$b" }
println(result) // [a1, b2, c3]
//sampleEnd
}

输出:

[a1, b2, c3]

用法八

infix fun <T, R> Iterable<T>.zip(
    other: Iterable<R>
): List<Pair<T, R>>

返回从具有相同索引的this 集合和other 集合的元素构建的对列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
println(listA zip listB) // [(a, 1), (b, 2), (c, 3)]
//sampleEnd
}

输出:

[(a, 1), (b, 2), (c, 3)]

用法九

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

使用提供的应用于每对元素的 transform 函数,返回从 this 集合和具有相同索引的 other 集合的元素构建的值列表。返回的列表具有最短集合的长度。

例子:



fun main(args: Array<String>) {
//sampleStart
val listA = listOf("a", "b", "c")
val listB = listOf(1, 2, 3, 4)
val result = listA.zip(listB) { a, b -> "$a$b" }
println(result) // [a1, b2, c3]
//sampleEnd
}

输出:

[a1, b2, c3]