kotlin中集合

fun main(arg: Array<String>) {

    //可读写的集合创建

    val mutableListOf1 = mutableListOf<Int>(1, 2, 3)
    val mutableListOf2 = mutableListOf<Int>()

    val mutableSetOf1 = mutableSetOf<Int>(11, 22, 33)
    val mutableSetOf2 = mutableSetOf<Int>()

    val mutableMapOf1 = mutableMapOf<Int, String>(Pair(1, "name1"), Pair(2, "name2"))
    val mutableMapOf2 = mutableMapOf<Int, String>()

    //可读不可写的集合创建
    val listOf1 = listOf<Int>(1, 2, 3)

    val of1 = setOf<Int>(1, 2, 3)

    val mapOf1: Map<Int, String> = mapOf<Int, String>(Pair(11, "name11"), Pair(22, "name22"))


    //list集合遍历
    println(" ---------------------------------mutableListOf -------------------------------------")
    mutableListOf2.add(100)//添加数据
    mutableListOf2.removeAt(0)//删除数据
    for (i in mutableListOf1) {
        if (i==1){

        }
        println(i)
    }
    println(" ------------------------------------listOf-----------------------------------------")
    val get = listOf1.get(0)
    println("索引0的值为"+get)
    for (i in listOf1) {
        println(i)
    }

    println(" ---------------------------------------mutableSetOf---------------------------------")
    mutableSetOf2.add(100)//添加数据
    mutableSetOf2.remove(11)//删除数据
    //set集合遍历
    for (i in mutableSetOf1) {
        println(i)
    }
    println(" -------------------------------------SetOf-----------------------------------")

    for (i in of1) {
        println(i)
    }
    println(" ------------------------------mutableMapOf------------------------------------------")

    //map 集合遍历
    for (entry in mutableMapOf1) {
        println(entry)
    }
    println(" --------------------------------mutableMapOf----------------------------------------")
    mutableMapOf2.put(3,"new name")
    mutableMapOf2.remove(2)
    for ((key, value) in mutableMapOf1) {

        println("key  = $key  value = $value")
    }
    println(" ------------------------------mapOf-------------------------------------------------")

    for (entry in mapOf1) {
        println(entry)
    }
    println(" --------------------------------mapOf-----------------------------------------------")

    for ((key, value) in mapOf1) {
        println("key  = $key  value = $value")
    }
}
val toList = mutableListOf1.toList()//将可读写集合转为只读集合

 

posted on 2018-12-20 10:22  LoaderMan  阅读(321)  评论(0编辑  收藏  举报

导航