从 MutableList<Int> 初始化 Array<Int>

Lec*_*ico 4 android realm kotlin

我有以下代码:

val realm = Realm.getDefaultInstance()
val items = realm.where(ItemRealm::class.java).equalTo("Id", id).findAll()
val ids = arrayOf<Int>(locations.map { it.locationId!! })
return realm.where(LocationRealm::class.java).`in`("id", ids).findAll()
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

类型推断失败。预期类型不匹配:必需的 Int,找到的列表

我知道这是因为数组构造函数的第一个参数是 Size,但我不知道如何初始化该数组。我需要它,因为Realm.where.in需要Array<Int>工作。

除了初始化以下方式之外,还有其他方式(更快)吗?

val locations = realm.where(ItemStockLocationsRealm::class.java).equalTo("stockId", id).findAll()
val ids = arrayOf(locations.size) {0}
for (i in locations.indices) { ids[i] = locations[i]?.locationId!! }
Run Code Online (Sandbox Code Playgroud)

Neo*_*Neo 5

val ids : Array<Int> = locations.map { it.locationId!! }.toTypedArray()
Run Code Online (Sandbox Code Playgroud)