arrayListOf和mutableListOf有什么区别,哪个更好? [英] What is the difference between arrayListOf and mutableListOf , which one is better?
问题描述
我试图在Kotlin中使用集合,并且感到困惑于arrayListOf和mutableListOf之间,我们应该使用哪一个?为什么?
I am trying to use collections in Kotlin and got confused between arrayListOf and mutableListOf, which one should we use and why?
推荐答案
都可以,调用两者之间的唯一区别就是表达您的意图.
Either is fine, the only difference between calling the two is expressing your intent.
ArrayList
是MutableList
的具体实现,而从Kotlin 1.1开始,mutableListOf
也返回ArrayList
.
ArrayList
is a concrete implementation of MutableList
, and mutableListOf
, as of Kotlin 1.1, also returns an ArrayList
.
您是否知道自己特别想要ArrayList,因为您有意识地决定这是适合您的应用程序的选择?您可能想要arrayListOf
(或直接调用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).
您是否只想要某种MutableList,并且可以很好地获得实现定义为正确的选择?只需使用mutableListOf
即可.
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.
这篇关于arrayListOf和mutableListOf有什么区别,哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!