- 与java不同,kotlin中所有类型转换必须显示调用方法:
val e: Int = 10
//val f: Long = e // implicitness not allowed
val f: Long = e.toLong()
- 字符串支持模板变量引用:
val j = “I❤️China”
println(“Value of String ‘j’ is: $j”) // no need brackets
println(“Length of String ‘j’ is: ${j.length}”) // need brackets
System.out.printf(“Length of String ‘j’ is: %d\n”, j.length)
-
===
比较引用,==
比较值:
val k = “Today is a sunny day.”
val m = String(“Today is a sunny day.”.toCharArray())
println(k === m) //比较引用
println(k == m) //比较值
val n = “”"
<!doctype html>
Hello World
This is a demo page.
“”".trimIndent()
println(n)
集合类型:
- 只有可变的才能添加删除元素:
val intList: List = listOf(1, 2, 3, 4) //不可变list 不能添加删除
val intList2: MutableList = mutableListOf(1, 2, 3, 4)//可变list 可以添加删除
val map: Map<String, Any> = mapOf(“name” to “benny”, “age” to 20)
val map2: Map<String, Any> = mutableMapOf(“name” to “benny”, “age” to 20)
- 集合遍历:
//集合遍历 for-i
for (i in 0…10) {
println(i)
}
//集合遍历 for-in
for (e in list) {
println(e)
}
var i = 0
while (i++ < 10) {
println(i)
}
do {
println(“Hello”)
} while (false)
//集合遍历 for-each
list.forEach {
if (it == 2) return@forEach //continue操作
println(it)
}
- 集合遍历可以直接通过
+
或-
号来添加/删除元素:
//直接仿java的创建方式也可以
val stringList = ArrayList
()
for (a in ‘a’…‘z’){
stringList.add(“char: $a”)
}
for (a in stringList){
println(“item====${a}”)
}
//kotlin集合添加元素,可以直接+,等价于add方法
for (i in 0 … 10){
stringList += “num: $i”
}
//kotlin集合删除元素,可以直接-,等价于remove方法
for (i in 0 … 10){
stringList -= “num: $i”
}
- 与java不同,可以直接通过
[ ]
下标索引赋值取值:
stringList[5] = “HelloWorld”
val valueAt5 = stringList[5]
- map也可以直接用
[ ]
来存取:
val hashMap = HashMap<String, Int>()
hashMap[“Hello”] = 10
println(hashMap[“Hello”])
- 特殊集合类型:Pair表示一对值, Triple表示一个三值集合
val pair = “Hello” to “Kotlin”
val pair = Pair(“Hello”, “Kotlin”)
val first = pair.first
val second = pair.second
val (x, y) = pair
val triple = Triple(“x”, 2, 3.0)
val first = triple.first
val second = triple.second
val third = triple.third
val (x, y, z) = triple
数组类型:
kotlin中的数组类型跟java相对应,其中基本类型和装箱类型的写法也是不一样的,装箱类型的都带有泛型参数,基本类型直接是一个类
- 数组的长度:
val a = IntArray(5)
println(a.size) //same with the Collections(e.g. List)
val b = ArrayList()
println(b.size)
比java好的地方:不需要.length和.size区分了,集合长度也是.size
- 数组创建
val c0 = intArrayOf(1, 2, 3, 4, 5)
val c1 = IntArray(5){ 3 * (it + 1) } // y = 3*(x + 1) 这里it是从0开始的下标
println(c1.contentToString())
- 引用数组值
val d = arrayOf(“Hello”, “World”)
d[1] = “Kotlin”
println("${d[0]}, ${d[1]}")
- 数组遍历:
val e = floatArrayOf(1f, 3f, 5f, 7f)
//普通遍历
for (element in e) {
println(element)
}
println("====================")
//遍历支持箭头函数
e.forEach { element ->
println(element)
}
println("====================")
//一个参数可以省略箭头 it是某个元素
e.forEach {
println(it)
}
println("====================")
显然forEach 和箭头函数的遍历方式最方便
- 判断某个元素是否在数组中:
if(1f in e){
println(“1f exists in variable ‘e’”)
}
- 判断某个元素不在数组中:
if(1.2f !in e){
println(“1.2f not exists in variable ‘e’”)
}
区间类型:
- kotlin中的区间类型,java没有:
- 可数区间 即离散区间才能打印出来,连续区间是不能打印的:
//闭区间
val intRange = 1…10 // [1, 10]
val charRange = ‘a’…‘z’
val longRange = 1L…100L
val floatRange = 1f … 2f // [1, 2]
val doubleRange = 1.0 … 2.0
//打印可数的区间 离散区间
println(intRange.joinToString())
//对于不可数的区间,这样不能打印出来 连续区间
println(floatRange.toString())
- 可数的离散区间才支持步长设置:
//可数的离散区间才支持步长设置
val intRangeWithStep = 1…10 step 2
val charRangeWithStep = ‘a’…‘z’ step 2
val longRangeWithStep = 1L…100L step 5
println(intRangeWithStep.joinToString())
println("====================")
//半闭半开区间
val intRangeExclusive = 1 until 10 // [1, 10)
val charRangeExclusive = ‘a’ until ‘z’
val longRangeExclusive = 1L until 100L
println(intRangeExclusive.joinToString())
println("====================")
//倒序区间
val intRangeReverse = 10 downTo 1 // [10, 9, … , 1]
val charRangeReverse = ‘z’ downTo ‘a’
val longRangeReverse = 100L downTo 1L
println(intRangeReverse.joinToString())
println("====================")
- 遍历离散区间跟数组一样:
for (element in intRange) {
println(element)
}
intRange.forEach {
ntRangeExclusive = 1 until 10 // [1, 10)
val charRangeExclusive = ‘a’ until ‘z’
val longRangeExclusive = 1L until 100L
println(intRangeExclusive.joinToString())
println("====================")
//倒序区间
val intRangeReverse = 10 downTo 1 // [10, 9, … , 1]
val charRangeReverse = ‘z’ downTo ‘a’
val longRangeReverse = 100L downTo 1L
println(intRangeReverse.joinToString())
println("====================")
- 遍历离散区间跟数组一样:
for (element in intRange) {
println(element)
}
intRange.forEach {