Kotlin中的通用类型的空可变集合(列表)

时间:2018-08-30 11:28:18

标签: collections kotlin

如何在Kotlin中声明一个空的通用类型的可变集合(列表)?

1 个答案:

答案 0 :(得分:1)

看看Kotlin reference regarding Collections: List, Set, Map。 下面将创建一个所需类型的空可变列表:

val yourCollection = mutableListOf<YourType>()

如果您想说的是可变的Collection<List<YourType>>,那么以下内容可能会对您有所帮助:

val mutableCollectionWithList : Collection<List<YourType>> = mutableListOf(mutableListOf())

或者,如果您确实不需要空白列表,也可以执行以下操作:

val yourCollection = mutableListOf(YourType(), ...)

如果您想要一个不可变的空列表,请使用以下内容:

val emptyList = emptyList<YourType>()
相关问题