我想添加/设置具有特定键值对的可变映射的元素.到目前为止,我发现我可以使用plus运算符和Pair数据类型添加新元素:
var arr3:Map<Any, Any> = mutableMapOf()
arr3 += Pair("manufacturer", "Weyland-Yutani")
//also, the "to" operator works too:
//arr3 += ("manufacturer" to "Weyland-Yutani")
Run Code Online (Sandbox Code Playgroud)
但是,我无法找到如何修改或添加新的键值对:
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
Run Code Online (Sandbox Code Playgroud)
你能详细说明怎么做吗?
我有可变地图private var optionsList: MutableMap<String, List<String>> = mutableMapOf(),我需要将它发送到另一个活动,我使用了这个:
val optionsIntent = Intent(this@MainActivity, OptionsActivity::class.java)
optionsIntent.putExtra(
"optionsLi",optionsList)
startActivity(optionsIntent)
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误putExtra,但我找不到任何类似于 putMap 或其他东西的东西。
我有一个可变的地图
val weeklyCheck = mutableMapOf(
Day.MONDAY to true,
Day.TUESDAY to true,
Day.WEDNESDAY to true,
Day.THURSDAY to true,
Day.FRIDAY to true,
Day.SATURDAY to true,
Day.SUNDAY to true
)
Run Code Online (Sandbox Code Playgroud)
如何将所有键设置为 false。目前我正在使用这样的东西,有没有更好的方法来做到这一点。
private fun resetDays() {
weeklyCheck.put(Days.MONDAY, false)
weeklyCheck.put(Days.TUESDAY, false)
weeklyCheck.put(Days.WEDNESDAY, false)
weeklyCheck.put(Days.THURDSAY, false)
weeklyCheck.put(Days.FRIDAY, false)
weeklyCheck.put(Days.SATURDAY, false)
weeklyCheck.put(Days.SUNDAY, false)
}
Run Code Online (Sandbox Code Playgroud)