我想用特定的键值对添加/设置可变映射的元素。 到目前为止,我想通了,我可以用加运营商对数据类型以及增加新的元素:Kotlin基础知识:如何添加或设置Map的元素?
var arr3:Map<Any, Any> = mutableMapOf()
arr3 += Pair("manufacturer", "Weyland-Yutani")
//also, the "to" operator works too:
//arr3 += ("manufacturer" to "Weyland-Yutani")
但是,我找不到如何修改或添加新键值对:
arr3["manufacturer"] = "Seegson" // gives an error(Kotlin: No set method providing array access)
arr3["manufacturer"] to "Seegson" // compiles, but nothing is added to the array
请您详细说明我该怎么做?
**“+ =运算符创建一个map的新实例并改变变量arr3”**当它,这种行为欺骗了我(错误的类型是我的错)。感谢您的详细解答! –