如何在 Kotlin 中初始化 List<T>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36896801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to initialize List<T> in Kotlin?
提问by Ali Dehghani
I see Kotlin has a List<out E>
collection and I was wondering about different ways to initialize one. In Java, I could write:
我看到 Kotlin 有一个List<out E>
集合,我想知道初始化一个集合的不同方法。在 Java 中,我可以这样写:
List<String> geeks = Arrays.asList("Fowler", "Beck", "Evans");
How can I achieve the same in Kotlin?
如何在 Kotlin 中实现相同的目标?
回答by Ilya
回答by gmariotti
Just for adding more info, Kotlin offers both immutable List
and MutableList
that can be initialized with listOf
and mutableListOf
. If you're more interested in what Kotlin offers regarding Collections, you can go to the official reference docs at Collections.
只是为了添加更多信息,Kotlin 提供了不可变的List
和MutableList
可以用listOf
和初始化的mutableListOf
。如果你更感兴趣的是科特林提供关于集合,你可以去官方参考文档集合。
回答by Magnilex
Both the upvoted answers by Ilyaand gmariottiare good and correct. Some alternatives are however spread out in comments, and some are not mentioned at all.
Ilya和gmariotti的赞成答案都很好且正确。然而,一些替代方案在评论中展开,有些则根本没有提及。
This answer includes a summary of the already given ones, along with clarifications and a couple of other alternatives.
这个答案包括对已经给出的答案的总结,以及澄清和其他一些替代方案。
Immutable lists (List
)
不可变列表 ( List
)
Immutable, or read-only lists, are lists which cannot have elements added or removed.
不可变或只读列表是不能添加或删除元素的列表。
- As Ilya points out,
listOf()
often does what you want. This creates an immutable list, similar toArrays.asList
in Java. - As frogcoderstates in a comment,
emptyList()
does the same, but naturally returns an empty list. listOfNotNull()
returns an immutable list excluding allnull
elements.
- 正如伊利亚指出的那样,
listOf()
经常做你想做的事。这将创建一个不可变的列表,类似于Arrays.asList
Java。 - 正如frogcoder在评论中指出的那样,
emptyList()
执行相同的操作,但自然会返回一个空列表。 listOfNotNull()
返回不包括所有null
元素的不可变列表。
Mutable lists (MutableList
)
可变列表 ( MutableList
)
Mutable lists can have elements added or removed.
可变列表可以添加或删除元素。
- gmariotti suggests using
mutableListOf()
, which typically is what you want when you need to add or remove elements from the list. - Greg Tgives the alternative,
arrayListOf()
. This creates a mutableArrayList
. In case you really want anArrayList
implementation, use this overmutableListOf()
. - For other
List
implementations, which have not got any convenience functions, they can be initialized as, for example,val list = LinkedList<String>()
. That is simply create the object by calling its constructor. Use this only if you really want, for example, aLinkedList
implementation.
- gmariotti 建议使用
mutableListOf()
,当您需要从列表中添加或删除元素时,这通常是您想要的。 - Greg T给出了替代方案,
arrayListOf()
。这将创建一个可变的ArrayList
. 如果您真的想要一个ArrayList
实现,请使用它 overmutableListOf()
。 - 对于其他
List
没有任何便利功能的实现,它们可以被初始化为,例如,val list = LinkedList<String>()
。这只是通过调用其构造函数来创建对象。仅当您确实需要(例如)LinkedList
实现时才使用它。
回答by CodeRanger
Let me explain some use-cases : let's create an immutable(non changeable) list with initializing items :
让我解释一些用例:让我们创建一个带有初始化项的不可变(不可更改)列表:
val myList = listOf("one" , "two" , "three")
let's create a Mutable (changeable) list with initializing fields :
让我们创建一个带有初始化字段的可变(可变)列表:
val myList = mutableListOf("one" , "two" , "three")
Let's declare an immutable(non changeable) and then instantiate it :
让我们声明一个不可变(不可更改)然后实例化它:
lateinit var myList : List<String>
// and then in the code :
myList = listOf("one" , "two" , "three")
And finally add some extra items to each :
最后为每个添加一些额外的项目:
val myList = listOf("one" , "two" , "three")
myList.add() //Unresolved reference : add, no add method here as it is non mutable
val myMutableList = mutableListOf("one" , "two" , "three")
myMutableList.add("four") // it's ok
回答by Nikhil Katekhaye
In this way, you can initialize the List in Kotlin
这样就可以在Kotlin中初始化List
val alphabates : List<String> = listOf("a", "b", "c")