Answers for "java.lang.UnsupportedOperationException when i try to set value in Muttablelist kotlin"

0

java.lang.UnsupportedOperationException when i try to set value in Muttablelist kotlin

You can fix this by calling toMutableList() api 
rather than using smart cast (as).

fun main(){
    val x : List<String> = listOf("foo", "bar", "baz")
    //val y: MutableList<String> = x as MutableList<String> throws UnsupportedOperationException
    val y: MutableList<String> = x.toMutableList()
    y.add("sha")
    println(y) // [foo, bar, baz, sha]
}
Posted by: Guest on December-25-2020

Code answers related to "java.lang.UnsupportedOperationException when i try to set value in Muttablelist kotlin"

Browse Popular Code Answers by Language