【发布时间】:2020-06-19 19:09:40
【问题描述】:
如何在 Kotlin 中创建具有固定大小的自定义对象数组?
喜欢吗?
ArrayList<CustomObject> array = new ArrayList<>()
还是?
CustomObject[] objects = new CustomObjects[5]
【问题讨论】:
如何在 Kotlin 中创建具有固定大小的自定义对象数组?
喜欢吗?
ArrayList<CustomObject> array = new ArrayList<>()
还是?
CustomObject[] objects = new CustomObjects[5]
【问题讨论】:
有两种选择:
arrayOfNulls(size: Int) - 创建一个特定大小的数组并将其填充为空引用。不能将此函数用于不可为空的类型。<init>(size: Int, init: (Int) -> T) 构造函数 - 创建一个特定大小的数组,并为数组中的每个索引运行提供的 lambda,从而为该索引创建一个元素。【讨论】: