有一个使用等数据传入地图的情况
Map<Pair<Int, Int>), String>
Pair<Int, Int>) // zero based star index, inclusive end index
to
displayString
在进行该过程时,需要为当前位置选择正确的displayString,如
var index = 0
for (data in dataList) {
/*
if the current index is within one of the pair<int, int>
then use the mapped displayString
*/
index++
}
在科特林最好的方式是什么?
不确定这是否是你的意思,但可能是这样的:
var index = 0
for (data in dataList) {
/*
if the current index is within one of the pair<int, int>
then use the mapped displayString
*/
val currentPair = data.key
if ((currentPair.first .. currentPair.second).contains(index)) {
// do something
}
index++
}
不过,如果你的意思是想要一个getter从包含int的映射中获得第一对,那么你可能想要这样的东西:
fun getByIndex(dataList: Map<Pair<Int,Int>, String>, index: Int): String? {
return dataList.firstOrNull {
(it.key.first .. it.key.second).contains(index)
}
}
使用until
而不是..
以具有独占的结束索引