我想用特定的鍵值對添加/設置可變映射的元素。 到目前爲止,我想通了,我可以用加運營商對數據類型以及增加新的元素: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」**當它,這種行爲欺騙了我(錯誤的類型是我的錯)。感謝您的詳細解答! –