android–Kotlin合并两个可空的可变列表

android–Kotlin合并两个可空的可变列表,第1张

概述valmutableList1:MutableList<TeamInvitationData?>?valmutableList2:MutableList<TeamInvitationData?>?addAll方法可以用于合并可空的可变列表但是,这里它会抛出编译时错误.例:valmap1=listOne?.map{TeamInvitationData(it)}valmap2=listTwo?.map{TeamInvi

val mutableList1: MutableList<TeamInvitationData?>?val mutableList2: MutableList<TeamInvitationData?>?

addAll方法可以用于合并可空的可变列表但是,这里它会抛出编译时错误.

例:

val map1 = ListOne?.map { TeamInvitationData(it) }val map2 = ListTwo?.map { TeamInvitationData(it) }map1.addAll(map2)

Type interface Failed ,Please try to specify type argument explicitly.

在这里,任何方式我可以合并这两个数组,提前谢谢.

解决方法:

这里有几个解决方案.

>如果您需要将所有元素添加到mutableList1:

val mutableList1: MutableList<Any?>? = ...val mutableList2: MutableList<Any?>? = ...mutableList1?.let { List1 -> mutableList2?.let(List1::addAll) }

>如果您需要新的可空列表作为结果:

val mutableList1: MutableList<Any?>? = ...val mutableList2: MutableList<Any?>? = ...val List3: List<Any?>? = mutableList1?.let { List1 ->    mutableList2?.let { List2 -> List1 + List2 }}

>如果您需要新的可空可变列表作为结果:

val mutableList1: MutableList<Any?>? = ...val mutableList2: MutableList<Any?>? = ...val List3: MutableList<Any?>? = mutableList1        ?.let { List1 -> mutableList2?.let { List2 -> List1 + List2 } }        ?.toMutableList()

>如果您需要新的非空列表作为结果:

val mutableList1: MutableList<Any?>? = ...val mutableList2: MutableList<Any?>? = ...val List3: List<Any?> = mutableList1.orEmpty() + mutableList2.orEmpty()
总结

以上是内存溢出为你收集整理的android – Kotlin合并两个可空的可变列表全部内容,希望文章能够帮你解决android – Kotlin合并两个可空的可变列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1111483.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存