如何在kotlin中声明泛型类型的空可变集合(List)?
看看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>()