Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试在 Kotlin 中使用集合,但对 arrayListOf 和 mutableListOf 感到困惑,我们应该使用哪一个,为什么?
两者都可以,调用两者之间的唯一区别是表达您的意图。
ArrayList是MutableList, 和的具体实现mutableListOf,从 Kotlin 1.1 开始,也返回一个ArrayList.
ArrayList
MutableList
mutableListOf
你知道你特别想要一个 ArrayList 是因为你有意识地决定这是你的应用程序的正确选择吗?您可能想要arrayListOf(或者直接调用ArrayList构造函数,如果该接口更适合您)。
arrayListOf
您是否只想要某种 MutableList,并且可以将实现定义为正确选择的任何内容都做好?改用就好mutableListOf了。