【问题标题】:Initialize Array<Int> from MutableList<Int>从 MutableList<Int> 初始化 Array<Int>
【发布时间】:2019-10-09 08:06:35
【问题描述】:

我有以下代码:

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

得到以下错误:

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

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

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

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!! }

【问题讨论】:

    标签: android kotlin realm


    【解决方案1】:
    val ids : Array<Int> = locations.map { it.locationId!! }.toTypedArray()
    

    【讨论】:

      猜你喜欢
      • 2016-03-03
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2021-03-19
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多