📜  Kotlin 集合写入操作

📅  最后修改于: 2022-05-13 01:55:49.213000             🧑  作者: Mango

Kotlin 集合写入操作

集合写入操作用于更改 MutableCollection 的内容。 MutableCollection被定义为具有添加和删除等写入操作的集合。

    支持的操作如下:
  1. 添加元素
  2. 删除元素和
  3. 更新元素

添加元素——

add()函数用于将元素添加到现有的可变集合组。它将对象附加到集合的末尾。
演示添加元素的 Kotlin 程序 -

//add a single element to a list or
//a set, use the add() function
fun main(args: Array) {
    val integers = mutableListOf(11, 12, 13, 14)
    integers.add(15)
    println(integers)
}

输出:

[11, 12, 13, 14, 15]


addAll()函数用于将列表的所有元素添加到可变集合中。参数可以是任何形式的 Iterable、Sequence 或 Array。
Kotlin 程序演示元素的添加——

//add multiple elements to a list
//or a set, use the addAll() function
fun main(args: Array) {
    val numbers = mutableListOf(1, 2, 5, 6)
    // add set elements to list
    numbers.addAll(2, setOf(2, 13, 14))
    println(numbers)
  
    // add array elements to list
    numbers.addAll(arrayOf(7, 8))
    println(numbers)
}

输出:

[1, 2, 2, 13, 14, 5, 6]
[1, 2, 2, 13, 14, 5, 6, 7, 8]


plusAssign (+=)运算符也用于在集合中添加元素。
Kotlin 程序演示元素的添加——

//add element to a list
//or a set, use the plus operator
fun main(args: Array) {
    val list = mutableListOf("one", "two", "seven")
    list += listOf("four", "five")
    println(list)
  
    list += "three"
    println(list)
}

输出:

[one, two, seven, four, five]
[one, two, seven, four, five, three]

去除元素——

remove()函数用于从可变集合中删除元素。它删除第一次出现的元素。如果列表中不存在元素,则不删除任何内容。
Kotlin 程序来演示元素的移除——

//remove element to a list
//or a set, use the remove() function
fun main(args: Array) {
    val numbers = mutableListOf(11, 22, 33, 44, 33)
    // removes first occurrence 33
    numbers.remove(33)
    println(numbers)
    // removes nothing
    numbers.remove(53)
    println(numbers)
}

输出:

[11, 22, 44, 33]
[11, 22, 44, 33]


removeAll()函数用于删除所有与参数匹配的元素。
Kotlin 程序来演示元素的移除——

//remove element to a list
//or a set, use the removeAll() function
fun main(args: Array) {
    val counting = mutableSetOf("one", "two", "three", "four")
    println(counting)
    // remove all the elements passed as an argument
    counting.removeAll(setOf("one", "two", "four"))
    println(counting)
}

输出:

[one, two, three, four]
[three]


减号 (- =)运算符也用于删除集合中的元素。
Kotlin 程序来演示元素的移除——

//remove element to a list or a set
fun main(args: Array) {
    val counting = mutableListOf("one", "two", "three", "three", "four")
    counting -= "three"
    println(counting)
    // removes the elements
    counting -= listOf("four", "five", "two")
    println(counting)
}

输出:

[one, two, three, four]
[one, three]


retainAll()函数也用于删除元素。它删除不满足条件的元素。
Kotlin 程序 –

//remove element to a list or a set, use the retainAll() function
fun main(args: Array) {
    val numbers = mutableListOf(11, 22, 33, 44)
    println(numbers)
    // only retain the numbers which satisfy the condition
    numbers.retainAll { it >= 33 }
    println(numbers)
}

输出:

[11, 22, 33, 44]
[33, 44]


clear()函数删除所有集合元素。
Kotlin 程序 –

//remove element to a list or a set, use the clear() function
fun main(args: Array) {
    val numbers = mutableListOf(12, 23, 34, 45)
    println(numbers)
    // it empty the list by removing all
    numbers.clear()
    println(numbers)
}

输出:

[12, 23, 34, 45]
[]

元素更新——

更新元素操作也是由 Mutable 集合提供的。

运算符[]用于替换给定位置的元素。
Kotlin 程序 –

//update element to a list 
fun main(args: Array) {
    val numbers = mutableListOf("one", "five", "three")
    numbers[1] =  "two"
    numbers[2] =  "two"
    println(numbers)
}

输出:

[one, two, two]


fill()函数用于将所有集合元素替换为指定值。
Kotlin 程序 –

//update element to a list
fun main(args: Array) {
    val numbers = mutableListOf(1, 2, 3, 4)
    // replaces all elements by 3
    numbers.fill(3)
    println(numbers)
    // replaces all elements by 0
    numbers.fill(0)
    println(numbers)
}

输出:

[3, 3, 3, 3]
[0, 0, 0, 0]