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

Initialize Array<Int> from MutableList<Int>

我有以下代码:

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()

出现以下错误:

Type inference failed. Expected type mismatch: Required Int, Found List

我知道这是因为 Array 构造函数的第一个参数是 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!! }
val ids : Array<Int> = locations.map { it.locationId!! }.toTypedArray()