📜  java.lang.UnsupportedOperationException 当我尝试在 Muttablelist kotlin 代码示例中设置值时

📅  最后修改于: 2022-03-11 14:53:33.625000             🧑  作者: Mango

代码示例1
You can fix this by calling toMutableList() api 
rather than using smart cast (as).

fun main(){
    val x : List = listOf("foo", "bar", "baz")
    //val y: MutableList = x as MutableList throws UnsupportedOperationException
    val y: MutableList = x.toMutableList()
    y.add("sha")
    println(y) // [foo, bar, baz, sha]
}