我有以下代码:
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()
关于android - 从 MutableList<Int> 初始化 Array<Int>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58299653/