What is the difference between arrayListOf and mutableListOf , which one is better? [duplicate]

大憨熊 提交于 2019-12-11 01:47:52

问题


I am trying to use collections in Kotlin and got confused between arrayListOf and mutableListOf, which one should we use and why?


回答1:


Either is fine, the only difference between calling the two is expressing your intent.

ArrayList is a concrete implementation of MutableList, and mutableListOf, as of Kotlin 1.1, also returns an ArrayList.

Do you know you specifically want an ArrayList because you have made the conscious decision that this is the right choice for your application? You probably want arrayListOf (or a direct call to the ArrayList constructor, if that interface works better for you).

Do you just want a MutableList of some sort, and are fine with getting whatever the implementation defines as the right choice? Just use mutableListOf instead.



来源:https://stackoverflow.com/questions/43182333/what-is-the-difference-between-arraylistof-and-mutablelistof-which-one-is-bett

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!