kotlin var list = ArrayList<JdInventoryNews>() 转换为 MutableList<JdInventoryNews>
时间: 2023-12-07 19:03:49 浏览: 33
可以使用 Kotlin 标准库中的 toMutableList() 函数将 ArrayList<JdInventoryNews> 转换为 MutableList<JdInventoryNews>,代码如下:
```kotlin
var list = ArrayList<JdInventoryNews>()
// 假设这里已经对 list 进行了添加元素的操作
var mutableList: MutableList<JdInventoryNews> = list.toMutableList()
```
这样就可以将 ArrayList 转换为 MutableList 了。
相关问题
kotlin var mutableList: MutableList<JdInventoryNews> = list.toMutableList() 和 val allList: MutableList<JdInventoryNews> = jdbcTemplate.query(findSql.toString(), BeanPropertyRowMapper(JdInventoryNews::class.java)) 获取两个list的差值
可以使用 Kotlin 的扩展函数 `subtract` 来获取两个 `MutableList` 的差值,示例如下:
```kotlin
// 定义两个 MutableList
var mutableList: MutableList<JdInventoryNews> = list.toMutableList()
val allList: MutableList<JdInventoryNews> = jdbcTemplate.query(findSql.toString(), BeanPropertyRowMapper(JdInventoryNews::class.java))
// 获取两个 MutableList 的差值
val diffList = mutableList.subtract(allList)
```
这样,`diffList` 就是 `mutableList` 中不在 `allList` 中的元素列表。
kotlin中String转为List<String>
在 Kotlin 中,你可以使用 `split` 函数将一个字符串拆分成一个字符串列表。这个函数接受一个分隔符作为参数,并返回一个字符串列表。
下面是将一个字符串转换为 `List<String>` 的示例代码:
```kotlin
val str = "Hello,World"
val list: List<String> = str.split(",")
```
在上述示例中,我们将字符串 `"Hello,World"` 按照逗号 `,` 进行拆分,并将结果存储在名为 `list` 的变量中。`list` 的类型为 `List<String>`。
如果你想按照空格拆分字符串,可以直接调用 `split` 函数而不传入任何参数:
```kotlin
val str = "Hello World"
val list: List<String> = str.split(" ")
```
这样就可以将字符串 `"Hello World"` 按照空格拆分成一个列表。