Kotlin Learning - Collection (List, MutableList)

Table of contents

1. List array

Two, MutableList variable array

3. For traversal of List


1. List array

1, getOrElse, getOrNull learning

The kotlin array can use the [] operator to get data, because kotlin uses the operator function. Try to use getOrElse or getOrNull to get elements to avoid the array out-of-bounds problem that often occurs in java.

fun main() {
    listTest()
}

fun listTest() {
    var l : List<String> = listOf<String>("ZhangSan" , "LiSi" , "WangWu" , "ZhaoLiu")
    println(l[0])
    println(l[1])
    println(l[2])
    println(l[3])
    //println(l[4]) 越界崩溃
    println(l.getOrElse(4) {"$it 你越界了哈!"})
    println()
    println(l.getOrNull(5))
}

/**
 打印值
ZhangSan
LiSi
WangWu
ZhaoLiu
4 你越界了哈!

null
 * */

1. The first parameter of getOrElse is the subscript value to be retrieved, the second parameter is a lambda function, and the return value is generic, which is the data type of list. When the input subscript is out of bounds, the return value of lambda will be returned without crashing.

2. getOrNull receives a parameter as the subscript to be retrieved, and returns null when the array is out of bounds, and will not crash.

Two, MutableList variable array

MutableList is a mutable collection, you can use operations such as add and remove, and you can use += -= operations for operator overloading.

removeIf, you can delete all elements of the specified type in batches, which is equivalent to traversing the collection to determine whether each element meets the criteria given by the lambda expression, and then delete the specified elements.

MutableList and List can be converted to each other. l.toList() l.toMutableList()


fun main() {
    val l : MutableList<String> = mutableListOf<String>("ZhangSan" , "LiSi" , "WangWu" , "ZhaoLiu")
    l.removeAt(1)
    l.add("零零散散")
    println("集合1 : $l")

    println()
    //运算符重载
    l += "张三"
    l += "王五"
    l += "张老六"

    l -= "ZhangSan"

    println("集合2 : $l")

    //测试getOrNull,数组越界
    println(l.getOrNull(600))

    //删除,条件是lambda表达式为true的元素,it为list中的每一个元素
    l.removeIf {
        it.contains("张")
    }

    println("集合3 : $l")

    l += "张三"
    l += "王五"
    l += "张老六"

    //遍历相关
    for (v in l) {
        print("元素 $ v , ")
    }

    println()

    l.forEach{
        print("元素:$it , ")
    }

    println()

    //forEachIndexed接收两个参数
    l.forEachIndexed{index , item ->
        print("元素:{$index} 值为 $item , ")
    }

    l.toList() ;
    l.toMutableList() ;

    println()
}

/**
 * 输出
 * 集合1 : [ZhangSan, WangWu, ZhaoLiu, 零零散散]

集合2 : [WangWu, ZhaoLiu, 零零散散, 张三, 王五, 张老六]
null
集合3 : [WangWu, ZhaoLiu, 零零散散, 王五]
元素 $ v , 元素 $ v , 元素 $ v , 元素 $ v , 元素 $ v , 元素 $ v , 元素 $ v ,
元素:WangWu , 元素:ZhaoLiu , 元素:零零散散 , 元素:王五 , 元素:张三 , 元素:王五 , 元素:张老六 ,
元素:{0} 值为 WangWu , 元素:{1} 值为 ZhaoLiu , 元素:{2} 值为 零零散散 , 元素:{3} 值为 王五 , 元素:{4} 值为 张三 , 元素:{5} 值为 王五 , 元素:{6} 值为 张老六 ,
 * */

3. For traversal of List

Very simple, mainly use until, .., downTo, step keywords, just look at the code


    //until 左关右开
    for (i in 0 until 50){
        print("$i ")//输出 0 - 49
    }
    println()
    //.. 左关右关
    for (i in 0..50){
        print("$i ")//输出 0 - 50
    }

    val ll = 0..50
    println(ll)

    //downTo自降方式,左闭右闭
    for (i in 50 downTo 0) {
        print("$i ")//输出 0 - 50
    }
    println()

    //step设置步长
    for (i in 50 downTo 0 step 2) {
        print("$i ")//输出 0 - 50
    }
    println()

    //遍历列表,直接使用until避免最后一个元素越界问题
    println("使用for加下标遍列表:")
    for (i in 0 until l.size) {
        print("${l[i]} , ")
    }
    println()
    //逆向遍历列表
    for (i in l.size-1 downTo 0) {
        print("${l[i]} , ")
    }

Guess you like

Origin blog.csdn.net/mldxs/article/details/127150691