Kotlin 基础
来源:互联网 发布:时标网络图 软件 编辑:程序博客网 时间:2024/06/11 21:48
Hello World
fun main(args: Array<String>) { println("Hello World!")}
基本数据类型
- Byte 1字节整数
- Short 2字节整数
- Int 4字节整数
- Long 8字节整数
- Float 4字节浮点数
- Double 8字节浮点数
- Boolean 布尔型
- Char 字符型
变量、常量声明
用var
声明变量,用val
声明常量,有初始值时类型可由编译器自动推断
var t:Int = 123var a = 123a = 345val A = 123A = 345 // errorval B:IntB = 123B = 345 //error// 标明类型时,变量默认不可空var c:Int = null // error c为不可空// 声明可空变量,在类型后加 ?var d:Int? = null
类型转换
只允许显式类型转换
toByte(): BytetoShort(): ShorttoInt(): InttoLong(): LongtoFloat(): FloattoDouble(): DoubletoChar(): ChartoString(): String
接收用户输入
println("你输入了:${readLine()}")
使用
java.util.Scanner
输入时,传入System.in
参数会产生错误,因为in
在kotlin中是保留字。使用反引号访问该字段(类似于在js中用obj[name]访问对象)val sc:Scanner = Scanner(System.in) // errorval sc:Scanner = Scanner(System.`in`) sc.nextInt()
控制流
while,do-while,if-else 正常使用
没有switch语句 使用when表达式代替
if-else 表达式
if-else中最后执行的表达式为返回值
var a = 1var b = if(a == 0) 123 else 456println(b) // 456
for-in
for循环只有for-in形式,没有for(;;)形式
遍历有迭代器的对象
val array = arrayOf(1,2,3,4,5)for (item in array) print(item)for (i in array.indices) { print(array[i])}for ((index, value) in array.withIndex()) { println("the element at $index is $value")}
when表达式
when 既可以被当做表达式使用也可以被当做语句使用。如果它被当做表达式, 符合条件的分支的值就是整个表达式的值,如果当做语句使用, 则忽略个别分支的值。
val x = 12// 增强switchwhen (x) { 1 -> print("x == 1") 2 -> print("x == 2") 3, 4 -> print("x == 3 or x == 4") in 1..10 -> print("x is in the range") !in 10..20 -> print("x is outside the range") is String -> x.startsWith("prefix") else -> { print("else") }}// 增强if-elsewhen { x.isOdd() -> print("x is odd") x.isEven() -> print("x is even") else -> print("x is funny")}
字符串
Kotlin字符串可以使用模版语法
val i = 123val s = "i = $i" // i = 123val s2 = "i = ${i}" // i = 123
使用3对引号创建原生字符串,会忽略转义字符,可以多行
模版语法正常使用
val i = 123val s = """afdsfasi = $i\n\t\n """ // \n\t\n不会被转义为换行和制表符
数组
使用Array
类表示
使用arrayOf()
创建数组,arrayOfNulls()
创建指定长度的null数组
val array = arrayOf(1, 2, 3, 4, 5) // [1, 2, 3, 4, 5] val nullArray = arrayOfNulls<Int>(5) // [null * 5] array[0] = 123 // 通过长度和初始化函数创建数组 val arr = Array(4, fun(index: Int): Int { return index }) // [0, 1, 2, 3]
函数
Lambda
{参数列表 -> 函数体}
- 参数列表为空时,可省略
->
- 只有一个参数时,可省略参数列表和
->
,该参数用it
访问 - 当函数的最后一个参数是Lambda时,可以把Lambda写在函数后
使用Lambda进行数组操作
val arr = arrayOf(2, 4, 5, 7, 10)// 遍历操作arr.forEach { print(it) } // 245710 // 带index的遍历操作arr.forEachIndexed { index, s -> println(" $index:$s") } // 0:2 1:4 2:5 3:7 4:10// 映射一个新数组(不改变原数组)arr.map { it * 2 }.forEach { print(it) } // 48101420// 累积计算print(arr.reduce {a, b -> a + b}) // 28//arr.reduceIndexed { index, a, b -> a + b } 带index的累积计算//arr.reduceRight arr.reduceRightIndexed 逆序累积计算
阅读全文
0 0
- Kotlin基础
- kotlin基础
- kotlin基础
- Kotlin基础
- kotlin基础
- kotlin基础
- kotlin基础
- Kotlin 基础
- Kotlin基础
- kotlin基础
- Kotlin-基础
- Kotlin基础
- Kotlin基础
- Kotlin基础语法
- Kotlin 基础语法学习
- Kotlin基础语法
- kotlin基础语言知识
- Kotlin基础入门
- 全栈工程师将会缔造下一个高薪群体
- Akka(18): Stream:组合数据流,组件-Graph components
- Java--a++与 ++a 与 a=a+1 与a+=1
- HDU 6129 Just do it (组合数)
- 线性分类器-Tumer Prediction
- Kotlin 基础
- HDU1029-Ignatius and the Princess IV
- 15_接水问题
- ifconfig
- 使用POI导入Excel文件信息
- 织梦DEDE网站程序动态化+静态化+伪静态设置(图文)教程
- html接入海康视频数据
- Eureka+ribbon 实现服务注册与发现和负载均衡
- Netty ByteBuf 释放注意事项