fun main(args: Array<String>){
var arr=arrayOf("333",1,2,"fuck","hello",true,1.5f);
println("Hello Worldxxx! arr firstelement¥${arr[0]}");
val language = if (args.size == 0) "EN" else args[0]
var switchResult= if(arr.size==0 ) "first" else if(arr.size==7) "7" else if(arr.size==8) "8" else "xxx";
var whenresult= when(switchResult){
"first"->"this is white"
"7"->println("what 's that fuck?")
// "7"->"this is 7 arr"
"6"-> "finr 6666"
// "6"-> println("zheli you 7ge ")
else->"error< result:"+switchResult+",and element.size:"+arr.size
}
println("whenresult: if return return fucktion , result str:kotlin.Unit"+whenresult)
for (name in arr) {
println("name:"+name+",or ${name}")
}
}
上面比较好奇的是 7->println()这个逻辑,为毛可以接受一个函数非字符串,发现这个函数返回的是kotlin.Unit 字符串。
for循环
var arr=arrayOf("333",1,2,"fuck","hello",true,1.5f);
for (name in arr) {
println("name:"+name+",or ${name}")
}
for (index in arr.indices){
println("index:"+index+",value:${arr[index]}")
}
数组元素是否包含的判断
···
if ("aaa" in array) // collection.contains(obj) is called
println("Yes: array contains aaa")
···
for循环范围
for (a in 1..5)
print("${a} ")
when 的用法
fun describeWhen(obj: Any): String = when (obj) {
1 -> "Int"
"Hello" -> "Greeting"
is Long -> "Long"
is Boolean -> "Boolean"
!is String -> "Not a string"
else -> "Unknown"
}
println(describeWhen(false)); //输出Boolean这个函数可以放到任意函数里面去。