KotlinAndroid高级技术Android开发

Kotlin 进阶之路2 区间与集合类型

2018-05-03  本文已影响64人  香沙小熊

Kotlin 进阶之路 目录

1.区间(Range)

- 0..100表示[0,100]
- 0 until 100 表示[0,100)
- i in 0..100 判断i是否在区间[0,100]中
val range: IntRange = 0..1024 //[0,1024]---闭区间
val range_exclusive: IntRange = 0 until 30 //[0,30)---半闭区间
val emptyRange: IntRange = 0..-1

fun main(args: Array<String>) {
    println(emptyRange.isEmpty())
    println(range.contains(100))
    println(50 in range)

    for (i in range_exclusive) {
        print("$i  ")
    }
}
true
true
true
0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  

2.数组类型

基本写法:
- val array:Array<String> = arrayOf(...)

基本类型的数组

为了避免不必要的装箱费和拆箱,基本类型的数组是定制的

Java Kotlin
int[] IntArray
short[] ShortArray
long[] LongArray
float[] FloatArray
double[] DoubleArray
char[] CharArray
基本操作
- print array[i] 输出第i个元素
- array[i] = "Hello" 给第i个成员赋值
- array.length 数组的长度
val arrayOfInt: IntArray = intArrayOf(1, 2, 3, 4, 5, 6, 7)
val arrayOfChar: CharArray = charArrayOf('H', 'e', 'l', 'l', 'o', ',', 'W', 'o', 'r', 'l', 'd', '!')
val arrayOfString:Array<String> = arrayOf("你好","美女")

fun main(args: Array<String>) {
    println(arrayOfInt.size)
    for (i in arrayOfInt){
        println(i)
    }
    println(arrayOfInt.slice(2..4))

    println(arrayOfChar.joinToString())
    println(arrayOfChar.joinToString(""))

    println(arrayOfString[0])
    arrayOfString[1] ="帅哥"
    println(arrayOfString[1])

}
1
2
3
4
5
6
7
[3, 4, 5]
H, e, l, l, o, ,, W, o, r, l, d, !
Hello,World!
你好
/**
 *集合类型
 *
 * 同类型的值的组合,根据整体特性分:
 * 1.有序可重复 - Array ,索引从0开始(index,i)
 * 2.无序不重复 - set
 * 3.无序可重复 - Map ,有唯一的键(key)
 * */
fun main(args: Array<String>) {
    //Array: Array<类型> 或arrayOf(元素一、元素二、元素三,...元素三)
    var country = arrayOf("China", "Japan", "American", "Germany", "China", "China")

    for (s in country) {
        println(s)
    }
    //创建一个有默认值的数组,Array(计算,{默认值})
    println("----------------创建一个有默认值的数组----------------")
    var countryPlaceHolder = Array(5, { "Australian" })
    for (s in countryPlaceHolder) {
        println(s)
    }


    //创建1到10的数组:Array(10,{i -> i+1})
    //i代表元素的索引值,从0开始
    println("----------------创建1到10的Array数组----------------")
    var oneToTen = Array(10, { i -> i + 1 })
    for (i in oneToTen) {
        println(i)
    }
    //元素计数:count,空否:isEmpty()
    println("元素计数:" + oneToTen.count())
    println("元素是否为空:" + oneToTen.isEmpty())

    //获取其中元素:数组名{索引},首元素:数组名.frist,尾元素:数组名.last
    //获取前5各元素的快捷方法.compoent 1到5
    println("${oneToTen.component1()},${oneToTen.component2()}")
    println("获取第三个元素" + oneToTen[2])
    println("获取最后一个元素" + oneToTen.last())

    //获取筛选重复后的数组:.distinct() 或用.toSet()转换为set。
    println("----------------获取筛选重复后的数组----------------")
    val countryNotRepeat = country.distinct()
    for (i in countryNotRepeat) {
        println(i)
    }
    println("----------------获取筛选重复后的set----------------")
    val countryNotRepeat2 = country.toSet()
    for (i in countryNotRepeat2) {
        println(i)
    }
    //切割数组:sliceArray
    println("----------------切割数组:sliceArray----------------")
    val countrySlice = country.sliceArray(2..3)
    for (s in countrySlice) {
        println(s)
    }

    //mutableList:MutableList<类型>或mutableListOf(元素1,元素2,...,元素n)
    //大小可变,类型不可变
    println("----------------新建mutableList----------------")
    var mutableCountry = mutableListOf("China", "Japan", "American", "Germany", "China", "China")

    for (s in mutableCountry) {
        println(s)
    }

    println("----------------移除首位置元素----------------")
    mutableCountry.removeAt(0)
    for (s in mutableCountry) {
        println(s)
    }



    //在末尾增加元素:add元素,添加另一个数组addAll方法
    println("----------------add添加元素----------------")
    mutableCountry.add("Korea")
    for (s in mutableCountry) {
        println(s)
    }
    val newCountry = arrayOf("Britain", "France")
    println("----------------整体添加元素----------------")
    mutableCountry.addAll(newCountry)
    for (s in mutableCountry) {
        println(s)
    }

    println("----------------移除新加的数组元素----------------")
    mutableCountry.removeAll(newCountry)
    for (s in mutableCountry) {
        println(s)
    }

    println("----------------获取筛选重复后的mutableList----------------")
    for (s in mutableCountry.distinct()) {
        println(s)
    }
}
China
Japan
American
Germany
China
China
----------------创建一个有默认值的数组----------------
Australian
Australian
Australian
Australian
Australian
----------------创建1到10的Array数组----------------
1
2
3
4
5
6
7
8
9
10
元素计数:10
元素是否为空:false
1,2
获取第三个元素3
获取最后一个元素10
----------------获取筛选重复后的数组----------------
China
Japan
American
Germany
----------------获取筛选重复后的set----------------
China
Japan
American
Germany
----------------切割数组:sliceArray----------------
American
Germany
----------------新建mutableList----------------
China
Japan
American
Germany
China
China
----------------移除首位置元素----------------
Japan
American
Germany
China
China
----------------add添加元素----------------
Japan
American
Germany
China
China
Korea
----------------整体添加元素----------------
Japan
American
Germany
China
China
Korea
Britain
France
----------------移除新加的数组元素----------------
Japan
American
Germany
China
China
Korea
----------------获取筛选重复后的mutableList----------------
Japan
American
Germany
China
Korea

3.Set集合

/**
 * 集合类型 Set:无序不重复
 * 主要方法 交差并补
 * */
fun main(args: Array<String>) {

    //Set<类型> 或 setOf(元素1,元素2,...,元素n)
    //大小不固定,元素类型不可变

    val country = setOf("Britain", "France", "China", "Japan", "American", "Germany")
    val countryAsia = setOf("China", "Japan", "Korea", "Vietnam")
    val countryPermanent = setOf("American", "China", "France", "Britain", "Russia")
    for (s in country) {
        println(s)
    }

    //元素计数:count(),空否:isEmpty()
    println("元素计数:" + country.count())

    //检查是否包含某个元素:contains,包含另一个set:containAll
    println("否包含China元素:" + country.contains("China"))

    //转换成数组:toTypedArray()
    println("----------------转换成数组:toTypedArray()----------------")
    val array = country.toTypedArray()
    for (s in array) {
        println(s)
    }
    //结合之间的运算:intersect/subtract/union/minus(补集的被操作对象无需是Set类型)
    println("----------------集合交集数据:intersect----------------")
    val interCountry = country.intersect(countryAsia)
    for (s in interCountry) {
        println(s)
    }
    println("----------------集合差集数据:subtract----------------")
    val subtractCountry = country.subtract(countryAsia)
    for (s in subtractCountry) {
        println(s)
    }
    println("----------------集合并集数据:union----------------")
    val unionCountry = country.union(countryAsia).union(countryPermanent)
    for (s in unionCountry) {
        println(s)
    }
    println("----------------集合补集数据:minus----------------")
    val minusCountry = country.minus(countryAsia)
    for (s in minusCountry) {
        println(s)
    }
    //MutableSet<类型>或mutableSetOf(元素1,元素2,...,元素n)
    //大小可变,类型不可变
    val mutableCountry = country.union(countryAsia).toMutableSet()
    println("----------------MutableSet----------------")
    for (s in mutableCountry) {
        println(s)
    }
    println("----------------MutableSet添加数据:add----------------")
    mutableCountry.add("Thailand")
    for (s in mutableCountry) {
        println(s)
    }
    println("----------------MutableSet添加集合数据:addAll----------------")
    val newCountry = setOf("Brazil","Mexico")
    mutableCountry.addAll(newCountry)
    for (s in mutableCountry) {
        println(s)
    }
    println("----------------MutableSet移除数据:remove----------------")
    //移除元素:remove,移除另一集合:removeAll
    mutableCountry.remove("Thailand")
    for (s in mutableCountry) {
        println(s)
    }
    println("----------------MutableSet移除集合数据:removeAll----------------")
    //移除集合元素:removeAll,移除另一集合:removeAll
    mutableCountry.removeAll(newCountry)

    for (s in mutableCountry) {
        println(s)
    }
}
Britain
France
China
Japan
American
Germany
元素计数:6
否包含China元素:true
----------------转换成数组:toTypedArray()----------------
Britain
France
China
Japan
American
Germany
----------------集合交集数据:intersect----------------
China
Japan
----------------集合差集数据:subtract----------------
Britain
France
American
Germany
----------------集合并集数据:union----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Russia
----------------集合补集数据:minus----------------
Britain
France
American
Germany
----------------MutableSet----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
----------------MutableSet添加数据:add----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Thailand
----------------MutableSet添加集合数据:addAll----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Thailand
Brazil
Mexico
----------------MutableSet移除数据:remove----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam
Brazil
Mexico
----------------MutableSet移除集合数据:removeAll----------------
Britain
France
China
Japan
American
Germany
Korea
Vietnam

4.Map集合

/**
 * 集合类型 Map :无序可重复,类似于"字典"的意思
 *
 * */
fun main(args: Array<String>) {

    //mapOf<Key,Value>(Pair(key,value),...)
    //显示指定类型,可防止初始化值类型的错误

    val products = mapOf<String, String>(Pair("IPhone", "6888"), Pair("Xiaomi", "3499"), Pair("Huawei", "5999"))

    //元素计数:size,空否:isEmpty
    println("元素计数:" + products.size)

    //获取某个key对应的value:get,getOrDefalt
    println("获取某个key对应的value:" + products.get("IPhone"))
    println("获取某个key对应的value,给定默认值:" + products.getOrDefault("ZTE", "0"))
    println("----------------获得map所有的key:keys----------------")
    //返回所有的key:keys,所有的value:values
    for (key in products.keys) {
        println(key)
    }
    println("----------------获得map所有的value:values----------------")
    for (value in products.values) {
        println(value)
    }

    //mutableMapOf<Key,Value>(Pair(key,value),...)
    val mutableProducts = products.toMutableMap()
    println("----------------获得map所有的value:values----------------")
    //添加或更新:下标方法 map变量名[key] = value
    mutableProducts["IPhone"]="7999"
    mutableProducts["Xiaomi"]="1999"
    for (mutableProduct in mutableProducts) {
        println("${mutableProduct.key},"+"${mutableProduct.value}")
    }
    
    
    println("----------------移除元素:remove----------------")
    //移除元素:remove
    mutableProducts.remove("IPhone")
    for (mutableProduct in mutableProducts) {
        println("${mutableProduct.key},"+"${mutableProduct.value}")
    }
    
    
    println("----------------添加元素:put----------------")
    //添加元素:remove
    mutableProducts.put("Samsung","6999")
    for (mutableProduct in mutableProducts) {
        println("${mutableProduct.key},"+"${mutableProduct.value}")
    }
}
元素计数:3
获取某个key对应的value:6888
获取某个key对应的value,给定默认值:0
----------------获得map所有的key:keys----------------
IPhone
Xiaomi
Huawei
----------------获得map所有的value:values----------------
6888
3499
5999
----------------获得map所有的value:values----------------
IPhone,7999
Xiaomi,1999
Huawei,5999
----------------移除元素:remove----------------
Xiaomi,1999
Huawei,5999
----------------添加元素:put----------------
Xiaomi,1999
Huawei,5999
Samsung,6999
上一篇下一篇

猜你喜欢

热点阅读