使用Map或Dictionary数据结构的Kotlin-Dictionaries和Map键/值对映射

时间:2019-07-19 10:50:24

标签: kotlin

import java.util.*

fun main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT. */

    val scanValue = Scanner(System.`in`)
    val n = scanValue.nextInt()

    var phoneBook = mutableMapOf<String, String>()

    for (i in 0 until n) {
        val name =  scanValue.next()
        val phoneNumber =    scanValue.next()

        if (!phoneBook.containsKey(name) && name != "") {
            phoneBook[name] = phoneNumber
        }
    }

    while (scanValue.hasNext()) {
        val names = scanValue.next()
        if (phoneBook.containsKey(names)) {
            println(names + "=" + phoneBook[names])
        } else {
            println("Not found")
        }
    }

    scanValue.close()

}

输入/输出: n = 3 电话簿包含下面的键值对(输入,以空格分隔)

Vijay 198120230012
Ravi 323323323233
Akash 34343434344

查询的行数未知

Rakesh
Not found
Vijay
Vijay=98120230012
Ravi
Ravi=323323323233
Nisha
Not found!

GitHub:https://github.com/vijay-mobilearchitect/Kotlin-Dictionaries-and-Maps Maps Key-Value pair

0 个答案:

没有答案