zip
所在位置是kotlin.collections.zip
,其相关用法介绍如下。
用法一
@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 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 <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>
@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]
用法三
@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 <R, V> BooleanArray.zip(
other: Iterable<R>,
transform: (a: Boolean, 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> 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>
@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]
用法六
返回从 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)]
用法七
使用提供的应用于每对元素的 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]
用法八
返回从具有相同索引的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)]
用法九
使用提供的应用于每对元素的 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]
相关用法
- Kotlin zipWithNext用法及代码示例
- Kotlin associateBy用法及代码示例
- Kotlin all用法及代码示例
- Kotlin map用法及代码示例
- Kotlin filterNot用法及代码示例
- Kotlin reduceRight用法及代码示例
- Kotlin Random.Default用法及代码示例
- Kotlin Byte.inc用法及代码示例
- Kotlin getValue用法及代码示例
- Kotlin Double.dec用法及代码示例
- Kotlin windowedSequence用法及代码示例
- Kotlin contentToString用法及代码示例
- Kotlin groupByTo用法及代码示例
- Kotlin commonPrefixWith用法及代码示例
- Kotlin MatchResult.Destructured用法及代码示例
- Kotlin Delegates.notNull用法及代码示例
- Kotlin ifBlank用法及代码示例
- Kotlin filterNotTo用法及代码示例
- Kotlin getOrPut用法及代码示例
- Kotlin Triple.<init>用法及代码示例
- Kotlin Duration.toString用法及代码示例
- Kotlin windowed用法及代码示例
- Kotlin indexOf用法及代码示例
- Kotlin reduceIndexed用法及代码示例
- Kotlin filterKeys用法及代码示例
注:本文由纯净天空筛选整理自kotlinlang.org大神的英文原创作品 kotlin.collections.zip。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。