How to add an item to an ArrayList in Kotlin?

后端 未结 4 1395
半阙折子戏
半阙折子戏 2021-01-31 01:13

How to add an item to an ArrayList in Kotlin?

4条回答
  •  醉梦人生
    2021-01-31 01:44

    For people just migrating from java, In Kotlin List is by default immutable and mutable version of Lists is called MutableList.

    Hence if you have something like :

    val list: List = ArrayList()
    

    In this case you will not get an add() method as list is immutable. Hence you will have to declare a MutableList as shown below :

    val list: MutableList = ArrayList()
    

    Now you will see an add() method and you can add elements to any list.

提交回复
热议问题