Kotlin学习:集合<1>.List的增、删、改、查

kotlin中的List集合分为可变和不可变,如果对集合中的数据没有增、删、改的需求,那么两种方式都可用。但是如果有增、删、改的需求,就只能声明可变List(MutableList)

我们可以用 listof() 函数创建一个不可变List,它有3个重载函数,创建的List都是不可变的

public inline fun <T> listOf(): List<T> = emptyList()

public fun <T> listOf(vararg elements: T): List<T> = if (elements.size > 0) elements.asList() else emptyList()

public fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)

我们可以点进listOf() 函数的返回值类型List中查看一下源码

public interface List<out E> : Collection<E> {
    override val size: Int
    override fun isEmpty(): Boolean
    override fun contains(element: @UnsafeVariance E): Boolean
    override fun iterator(): Iterator<E>

    override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean

    public operator fun get(index: Int): E

    public fun indexOf(element: @UnsafeVariance E): Int

    public fun lastIndexOf(element: @UnsafeVariance E): Int

    public fun listIterator(): ListIterator<E>

    public fun listIterator(index: Int): ListIterator<E>

    public fun subList(fromIndex: Int, toIndex: Int): List<E>
}

从源码中可以看出,不可变List只有查找函数,而没有add(),remove(),clear(),set()等修改集合元素的函数。所以在平时使用List集合的时候,需要根据自己的需求来声明List集合。本人在学kotlin的过程中,就犯了这样的错误,使用listof()函数创建了一个集合,却怎么也找不到add()函数,困惑了好久!

与不可变List相对应的自然就是可变List(MutableList)了。在MutableList中,除了继承List中的那些函数外,另外新增了add/addAll、remove/removeAll/removeAt、set、clear、retainAll等更新修改的操作函数。
创建一个MutableList的对象实例跟List类似,前面加上前缀 mutable

 val mutableList = mutableListOf(1, 2, 3)
    println(mutableList)
 输出:
[1, 2, 3]

另外,我们也可以直接使用Kotlin封装的 arrayListOf 函数来创建一个可变List:

val arrayList= arrayListOf(1, 2, 3)
 println(arrayList)
 输出:
[1, 2, 3]

虽然,kotlin将List集合分为可变和不可变,但是它们之间也是可以互相转换的

    //创建一个不可变List
    val imList = listOf(4, 5, 6)
    //将不可变List转换为可变List
    imList.toMutableList()

     //创建一个可变List
    val mutableList = mutableListOf(1, 2, 3)
    //将一个可变List转换为不可变List
    mutableList.toList()
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据你提供的代码,我看不出来具体的问题出在哪里,因为你没有明确说明什么是预期结果,什么是实际结果,也没有说出来你尝试过什么。不过我可以根据常见的问题提供一些可能的解决方案: 1. `JdInventoryNews` 类没有实现 `equals` 和 `hashCode` 方法,导致 `subtract` 方法无法正确比较两个对象是否相等。你可以在 `JdInventoryNews` 类中实现这两个方法,例如: ```kotlin data class JdInventoryNews(val id: Int, val title: String) { override fun equals(other: Any?): Boolean { if (this === other) return true if (other !is JdInventoryNews) return false return id == other.id && title == other.title } override fun hashCode(): Int { var result = id result = 31 * result + title.hashCode() return result } } ``` 2. `subtract` 方法返回的是一个新的集合,原有的集合并没有发生变化。如果你想要修原有的集合,需要使用 `removeAll` 方法。例如: ```kotlin mutableList.removeAll(allList) ``` 3. `subtract` 方法不会对集合进行排序,因此返回的结果也不是有序的。如果你需要有序的结果,可以使用 `sorted` 方法对两个集合进行排序,再使用 `subtract` 方法。例如: ```kotlin val sortedMutableList = mutableList.sorted() val sortedAllList = allList.sorted() val diffList = sortedMutableList.subtract(sortedAllList) ``` 希望这些提示能帮到你解决问题。如果还有其他问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值