gpt4 book ai didi

android - 将数组缩减为单个整数会导致类型错误

转载 作者:行者123 更新时间:2023-11-29 16:33:12 25 4
gpt4 key购买 nike

我正在尝试获取一组 Pairs(StartTime:Int, EndTime:Int) 并将它们减少为每个 session 的时间累积。

fun main() {
val sessionsInSecond = listOf<Pair<Int,Int>>(Pair(10,12), Pair(10,15))

val timeSpan: Int = sessionsInSecond.reduce{acc, it -> acc + (it.second - it.first) }
println(timeSpan)
}

这给了我以下错误:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: @InlineOnly public inline operator fun BigDecimal.plus(other: BigDecimal): BigDecimal defined in kotlin @InlineOnly public inline operator fun BigInteger.plus(other: BigInteger): BigInteger defined in kotlin public operator fun Array.plus(elements: Array): Array defined in kotlin.collections public operator fun Array.plus(elements: Collection): Array defined in kotlin.collections public operator fun Array.plus(element: Int): Array defined in kotlin.collections public operator fun BooleanArray.plus(element: Boolean): BooleanArray defined in kotlin.collections public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray defined in kotlin.collections public operator fun BooleanArray.plus(elements: Collection): BooleanArray defined in kotlin.collections public operator fun ByteArray.plus(element: Byte): ByteArray defined in kotlin.collections public operator fun ByteArray.plus(elements: ByteArray): ByteArray defined in kotlin.collections public operator fun ByteArray.plus(elements: Collection): ByteArray defined in kotlin.collections @InlineOnly public inline operator fun Char.plus(other: String): String defined in kotlin.text public operator fun CharArray.plus(element: Char): CharArray defined in kotlin.collections public operator fun CharArray.plus(elements: CharArray): CharArray defined in kotlin.collections public operator fun CharArray.plus(elements: Collection): CharArray defined in kotlin.collections public operator fun DoubleArray.plus(element: Double): DoubleArray defined in kotlin.collections public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray defined in kotlin.collections public operator fun DoubleArray.plus(elements: Collection): DoubleArray defined in kotlin.collections public operator fun FloatArray.plus(element: Float): FloatArray defined in kotlin.collections public operator fun FloatArray.plus(elements: FloatArray): FloatArray defined in kotlin.collections public operator fun FloatArray.plus(elements: Collection): FloatArray defined in kotlin.collections public operator fun IntArray.plus(element: Int): IntArray defined in kotlin.collections public operator fun IntArray.plus(elements: IntArray): IntArray defined in kotlin.collections public operator fun IntArray.plus(elements: Collection): IntArray defined in kotlin.collections public operator fun LongArray.plus(element: Long): LongArray defined in kotlin.collections public operator fun LongArray.plus(elements: LongArray): LongArray defined in kotlin.collections public operator fun LongArray.plus(elements: Collection): LongArray defined in kotlin.collections public operator fun ShortArray.plus(element: Short): ShortArray defined in kotlin.collections public operator fun ShortArray.plus(elements: ShortArray): ShortArray defined in kotlin.collections public operator fun ShortArray.plus(elements: Collection): ShortArray defined in kotlin.collections public operator fun String?.plus(other: Any?): String defined in kotlin public operator fun String?.plus(other: Any?): String defined in kotlin public operator fun Collection.plus(elements: Array): List defined in kotlin.collections public operator fun Collection.plus(elements: Iterable): List defined in kotlin.collections public operator fun Collection.plus(elements: Sequence): List defined in kotlin.collections public operator fun Collection.plus(element: Int): List defined in kotlin.collections public operator fun Iterable.plus(elements: Array): List defined in kotlin.collections public operator fun Iterable.plus(elements: Iterable): List defined in kotlin.collections public operator fun Iterable.plus(elements: Sequence): List defined in kotlin.collections public operator fun Iterable.plus(element: Int): List defined in kotlin.collections public operator fun Map.plus(pairs: Array>): Map defined in kotlin.collections public operator fun Map.plus(pair: Pair): Map defined in kotlin.collections public operator fun Map.plus(pairs: Iterable>): Map defined in kotlin.collections public operator fun Map.plus(map: Map): Map defined in kotlin.collections public operator fun Map.plus(pairs: Sequence>): Map defined in kotlin.collections public operator fun Set.plus(elements: Array): Set defined in kotlin.collections public operator fun Set.plus(elements: Iterable): Set defined in kotlin.collections public operator fun Set.plus(elements: Sequence): Set defined in kotlin.collections public operator fun Set.plus(element: Int): Set defined in kotlin.collections public operator fun Sequence.plus(elements: Array): Sequence defined in kotlin.sequences public operator fun Sequence.plus(elements: Iterable): Sequence defined in kotlin.sequences public operator fun Sequence.plus(elements: Sequence): Sequence defined in kotlin.sequences public operator fun Sequence.plus(element: Int): Sequence defined in kotlin.sequences

有什么解决办法的建议吗?附言。这是一个 reduce 练习,所以我不想只在 forEach 循环中添加值。

最佳答案

我猜你的结果应该是 Int这里?您可以将其更改为 fold让它毫不费力地工作:

val timeSpan: Int = sessionsInSecond.fold(0) { acc, it -> 
acc + (it.second - it.first)
}

您从 0 开始并继续添加到它,直到访问了所有值。您的代码中的问题是 acc类型为 Pair<Int,Int>而不是 Int

关于android - 将数组缩减为单个整数会导致类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53650146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com