嘿,我有嵌套串列,我想找到內部串列和外部串列的最后一次出現索引值。我嘗試使用 Kotlin 在陣列中查找最后一次出現的字串。我怎樣才能以有效的方式在我的情況下做到這一點。
團體
data class Group(
val key: Int,
val value: MutableList<GroupValue?>
)
組值
data class GroupValue(
val isRead: Boolean? = null,
val id: String? = null
)
健康)狀況
isRead != true
例如
場景一
val value = listOf(
Group(0, mutableListOf(GroupValue(true, "1"))),
Group(1, mutableListOf(GroupValue(true, "2"))),
Group(2, mutableListOf(GroupValue(false, "3"))),
Group(3, mutableListOf(GroupValue(true, "4"))),
Group(4, mutableListOf(GroupValue(false, "5"))),
Group(5, mutableListOf(GroupValue(true, "6"))),
)
例外輸出
inner list index is 0 and outer list index is 4
場景二
val value = listOf(
Group(0, mutableListOf(GroupValue(true, "1"))),
Group(1, mutableListOf(GroupValue(true, "2"))),
Group(2, mutableListOf(GroupValue(false, "3"))),
Group(3, mutableListOf(GroupValue(true, "4"))),
Group(4, mutableListOf(GroupValue(false, "5"))),
Group(5, mutableListOf(GroupValue(true, "6"))),
Group(6, mutableListOf(GroupValue(true, "6"),GroupValue(false, "7"))),
Group(7, mutableListOf(GroupValue(true, "7")))
)
例外輸出
inner list index is 1 and outer list index is 6
這里面查找有效的方式第一次出現的索引值在科特林我問第一個指標,但現在我需要在最后出現。
提前致謝。
uj5u.com熱心網友回復:
您可以為此使用indexOfLast和indexOfFirst功能
var innerIndex = -1
val outerIndex: Int = value.indexOfLast{ group ->
innerIndex = group.value.indexOfFirst { groupValue -> groupValue != null && groupValue.isRead != true }
return@indexOfLast innerIndex != -1
}
if(outerIndex != -1) {
// match found
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/323382.html
