Kotlin MutableList

Kotlin MutableList,MutableList是List集合中的可变集合,MutableList<E>接口继承于List<E>接口和Mutable Collection<E>接口,增加了对集合中元素的添加及删除的操作。可变MutableList集合是使用mutableListOf()函数来创建对象的,具体代码如下:

val muList: MutableList<Int> = mutableListOf(1, 2, 3)

上述代码中创建了一个可变的List集合,这个集合与不可变集合的操作类似,具体如下。

查询操作

MutableList集合的查询操作与List集合一样,主要有判断集合是否为空、获取集合中元素的数量以及返回集合中的元素的迭代器,相关方法如表所示。
Kotlin MutableList

接下来我们通过一个案例来演示上述表中List集合的查询操作,具体代码如下所示。

package com.itheima.chapter05
fun main(args: Array<String>) {
    val muList: MutableList<Int> = mutableListOf(5, 6, 7)
    if (muList.isEmpty()) {
        println(" 集合中没有元素")
        return
    } else {
        println(" 集合中有元素,元素个数为:" + muList.size)
    }
   if (muList.contains(5)) {
       println(" 集合中包含元素5")
   }
   println(" 遍历集合中的所有元素:")
   val mIndex = muList.iterator()  // 获取集合中元素的迭代器
   while (mIndex.hasNext()) {
       print(mIndex.next().toString() + "\t")
   }
}
运行结果:
集合中有元素,元素个数为:3
集合中包含元素5
遍历集合中的所有元素:
5 6 7

修改操作

MutableList集合可以对该集合中的元素进行修改操作,这些修改操作主要有向集合中添加一个元素、在指定位置添加元素、移除一个元素、移除指定位置的元素以及替换指定位置的元素,相关方法如表所示。
Kotlin MutableList

接下来我们通过一个案例来演示上述表中的一些修改操作,具体代码如下所示。

package com.itheima.chapter05
fun main(args: Array<String>) {
    val muList: MutableList<Int> = mutableListOf(1, 2, 3)
    muList.add(4)
    println(" 向集合中添加元素4:" + muList)
    muList.remove(1)
    println(" 移除集合中的元素1:" + muList)
    val a = muList.set(1, 7)
    println(" 用7 替换索引为1 的元素:" + muList)
   muList.add(1, 5)
   println(" 在索引为1 处添加元素5:" + muList)
   muList.removeAt(2)
   println(" 移除索引为2 的元素:" + muList)
}

运行结果:

向集合中添加元素4:[1, 2, 3, 4]
移除集合中的元素1:[2, 3, 4]
用7替换索引为1的元素:[2, 7, 4]
在索引为1处添加元素5:[2, 5, 7, 4]
移除索引为2的元素:[2, 5, 4]

批量操作

MutableList集合的批量操作主要有判断集合中是否包含另一个集合、向集合中添加一个集合、移除集合中的一个集合以及将集合中的所有元素清空,相关方法如表所示。
Kotlin MutableList

接下来我们通过一个案例来演示上述表中的一些批量操作,具体代码如下所示。

package com.itheima.chapter05
fun main(args: Array<String>) {
    val muList1: MutableList<String> = mutableListOf("北京", "上海")
    val muList2: MutableList<String> = mutableListOf("北京", "上海",
                                                      "深圳")
    muList2.removeAll(muList1)
    println("muList2 移除muList1后的元素为:" + muList2)
    muList2.addAll(muList1)
    println("muList2 添加muList1后的元素为:" + muList2)
   if (muList2.retainAll(muList1)) {
       println("muList2 包含muList1")
   } else {
       println("muList2 不包含muList1")
   }
   muList2.clear()
   println("muList2 清除所有元素后,该集合的元素个数为:" + muList1.size)
}

运行结果:

muList2移除muList1后的元素为:[深圳]
muList2添加muList1后的元素为:[深圳,北京,上海]
muList2包含muList1
muList2清除所有元素后,该集合的元素个数为:2

遍历操作

MutableList集合中的遍历操作主要有返回一个集合的迭代器与返回从指定位置开始的集合的迭代器,如表所示。

获取迭代器的方法 方法的具体含义
listIterator():MutableListIterator<E> 返回一个集合的迭代器
listIterator(indexInt):MutableListIterator<E> 从指定位置开始,返回结合的迭代器

接下来我们通过一个案例来演示上述表中迭代器的操作,具体代码如下所示。

package com.itheima.chapter05
fun main(args: Array<String>) {
    val muList: MutableList<String> = mutableListOf(" 春天", "夏天",
                                                              " 秋天", "冬天")
    val iterator1 = muList.listIterator()   //获取集合的迭代器
    val iterator2 = muList.listIterator(1) // 获取从位置1 开始的集合的迭代器
    println(" 遍历集合中的元素:")
    while (iterator1.hasNext()) {
        print(iterator1.next() + "\t")
   }
   println("\n" + " 从索引为1 处开始遍历集合中的元素:")
   while (iterator2.hasNext()) {
       print(iterator2.next() + "\t")
   }
}

运行结果:

遍历集合中的元素:
春天 夏天 秋天 冬天
从索引为1处开始遍历集合中的元素:
夏天 秋天 冬天

酷客教程相关文章:

赞(0)

评论 抢沙发

评论前必须登录!