代码之家  ›  专栏  ›  技术社区  ›  Martin Erlic

如何从键/值对列表中检索值,以及如何使用给定对中的键?

  •  0
  • Martin Erlic  · 技术社区  · 6 年前

    我有以下几对

    val mItemArray : ArrayList<Pair<Long, String>> = arrayListOf()
    
    I/System.out: [Pair{256 yeet}, Pair{128 yeah_boy}, Pair{64 spaghet}, Pair{32 screaming_kid}, Pair{16 nice}, Pair{8 leeroy}, Pair{4 wow}, Pair{2 damn_son}, Pair{1 baby_a_triple}]
    

    var index = 0
    var uniqueID = 1L
    for (item in rawItems) {
        mItemArray.add(index, Pair(uniqueID, item.name))
        println(item.name)
        index += index
        uniqueID += uniqueID
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   jaychang0917    6 年前

    你可以用 find() :

    mItemArray.find { it.first == id }?.second