Kotlin常用数据类型整理

整理了一下Kotlin常用的数据类型

import java.io.File
import java.net.URL

fun main() {
    //2020-3-9 Kotlin数据类型

    //#####整数类型#####
    val num1:Byte = 1           //位宽度8
    val num2:Short = 12           //位宽度16
    val num3:Int = 1234           //位宽度32
    val num4:Long = 131245L          //位宽度64

    println("Int最大值:" + Int.MAX_VALUE + "\nInt最小值:" + Int.MIN_VALUE)

    //#####浮点数类型#####
    val num5:Float = 2.0F            //位宽度32
    val num6:Double = 2.0           //位宽度64
    val num7:Double = 4E-3
    println("Double最大值:" + Double.MAX_VALUE + "\nDouble最小值:" + Double.MIN_VALUE)

    //显式转换
    val num8:Double = num5.toDouble()
    val num9:Long = num3.toLong()
    val num10:Int = (1.034f).toInt()

    //#####字符类型#####
    val char1:Char = 'a'  //单字符,用单引号
    val str1:String = "abc" //字符串,双引号

    val str2:String = "$str1${num10}" //等价于 “abc1”
    val strPlus:String = "a" + str2 //等价于 “aabc1”

    val char2:Char = str1[0]
    val char3:Char = "abcdefg"[1]

    //显式转换
    val str3:String = char1.toString()
    val str4:String = 'a'.toString()

    //#####布尔值#####
    val boo1:Boolean = true
    val boo2:Boolean = false

    //显式转换
    val boo3:Boolean = "true".toBoolean()
    val str5:String = boo3.toString()

    //#####上述类型皆可推断#####
    val boo4 = true
    val char4 = 'd'
    val str6 = "hhhhh"


    //#####数组#####
    //长度不可变

    //标准模式
    val str_array:Array<String> = arrayOf("abc","cde")
    val str_array3 = arrayOf<String>("yjk","cte") //类型后置
    val str_array2 = arrayOf("yjk","cte") //类型可省略,自动推断

    //工厂模式,匿名函数模式
    val int_array:Array<Int> = Array(3,{it->it*3})
    val int_arrayX = Array<Int>(3,{it->it*3})

    //直接类型模式
    val int_array2:IntArray = intArrayOf(11,53,65,23)
    int_array2[1] = 56435 //改变元素

    //显式转换
    val str7:String = int_array.toString()
    val list1:List<Int> = int_array.toList()


    //#####集合#####
    //无序,元素不可重复,只读

    val set1:Set<String> = setOf("asd","fgh")//固定长度的集合
    val set2:MutableSet<Int> = mutableSetOf(123,3445,5476,2345)//不固定长度的集合

    val set3:Set<Int> = setOf(1234,435,123) + set2 //取并集
    val set4:Set<Int> = setOf(1234,435,123) - set2 //取补集

    set2.remove(123) //移除某个元素
    set2.add(124315) //增加一项

    //遍历集合,迭代器不实用没写了(涉及指针)

    //内部函数
    set2.forEach {it ->
        //println(it)
    }

    //for循环
    for (i in set2){
        //println(i)
    }


    //#####列表/队列#####
    //有序,元素可重复,读写
    val list2:List<String> = listOf("aa","be","cs")//固定长度的列表
    val list3:MutableList<String> = mutableListOf("aca","bxe","cas")//不固定长度的列表

    val list4:MutableList<String> = (list2 + list3).toMutableList() //列表相加,显性转换一下

    list4.get(1) //得到第 2 号元素
    list4[1] //同上

    list4[1] = "421" //改变第二号元素
    list4.set(1,"421") //同上

    list4.remove("421") //移除”421“元素
    list4.removeAt(1)   //移除第二号元素

    list4.add("456") //在队末添加元素
    list4.add(1,"456") //指定位置添加元素

    //列表排序
    val list5 = list4.sortBy { it-> it.length } //条件升序排列
    val list6 = list4.sortByDescending { it-> it.length } //条件降序排列


    //前两种遍历与集合相似,不重复写了

    //indeices是列表下标的数组。例如列表长度为10,下标数组为0到9
    for (i in list4.indices){
        //println(list4[i])
    }


    //#####映射/字典#####
    //<KEY,VALUE> 无序,读写,key唯一
    //Pair方式太复杂,不写了
    val map1:Map<String,String> = mapOf("name" to "jack","age" to "16")//固定长度的字典
    val map2:MutableMap<String,Int> = mutableMapOf("temp1" to 12,"temp2" to 24)//不固定长度的字典

    val map3:Map<String,String> = mapOf("year" to "2010","age" to "16")//固定长度的字典
    val map4 = map1 + map3//字典合并

    map1["name"] //通过key得到value
    map1.get("name") //同上

    map2.put("temp1",78) //覆盖修改
    map2.put("temp3",17) //添加

    map2.remove("temp1")

    println(map2.containsValue(24)) //是否包含value
    println(map2.containsKey("temp1"))//是否包含key

    //遍历

    //foreach遍历需要较高API版本,支持api24以上,不建议使用
    map2.forEach {key,value ->
        //println(key + ":" + value)
    }

    for (i in map2.keys){
        //println(map2[i])
    }

    for ((k,v) in map2){
        //println(k + ":" + v)
    }

//上面是大部分常用的基本类型

    //URL和File是常用类型

    val file:File = File("a.txt")
    val url:URL = URL("https://www.baidu.com")

    //val contentStr = file.readText()
    //val contentByte = url.readBytes()

    //file.writeText("abcdefg")
    //file.writeBytes(contentByte)
}