kotlin note
kotlin基本数据类型6种:Byte Short Int Float Long Double java基本数据类型8种: byte short int float long double char boolean
kotlin中无三元运算符,用 if(a>b) a else b 代替
if (obj is String) = return obj.length // obj
在该条件分支内自动转换成 String
val x: String = y as String //若y为null,则抛出异常
val x: String? = y as String? //不会抛出异常
val x: String? = y as? String //as?在失败时返回 null
迭代: for (item in collection) print(item)
val items = listOf("apple", "banana", "kiwifruit")
for (index in items.indices) {
println("item at
通过索引遍历数组或list
for (i in array.indices) {
println(array[i])
}
for循环迭代 val numbers = listOf("one", "two", "three", "four") for (item in numbers) { println(item) }
forEach迭代 val numbers = listOf("one", "two", "three", "four") numbers.forEach { println(it) }
标准迭代: val numbers = listOf("one", "two", "three", "four") val numbersIterator = numbers.iterator() while (numbersIterator.hasNext()) { println(numbersIterator.next()) }