前言
本文主要介绍kotlin中控制流的简单使用
if-else
if..else 在Java中是比较常见的条件判断语句,根据一定的条件执行响应的代码,那么kotlin中的if-else的使用方法如下
if(x>2){
Log.e("if",">2")
}else{
Log.e("if","<=2")
}
打印结果
11-05 09:43:49.848 25271-25271/? E/if: <=2
在使用上与Java的区别不大,但Kotlin的if-else能够充当表达式,实现三目表达式的功能。
var flag=if (x>2) println(">2") else println("<=2")
测试结果
11-05 09:48:12.621 25442-25442/com.zhqy.javademo I/System.out: <=2
when
kotlin中的when类似于Java中的switch,但他的功能比switch更为强大和表达明确
when(x){
2->Log.e("when","2")
in 2..Int.MAX_VALUE->Log.e("when",">2")
in Int.MIN_VALUE..1->Log.e("when","<2")
}
测试结果
11-05 09:57:42.014 26122-26122/com.zhqy.javademo E/when: 2
其中in代表x的取值范围。
When也可以不传入参数,这时when的写法更为自由,可以判断任意类型的数据,写法也更为简单
var y:Int =2
var str:String="你好啊"
var double:Double=2.0
when{
y>=1&&y<=2->Log.e("when","in 1..2")
"你大爷".equals(str)->Log.e("when","相等")
double==2.0->Log.e("when","double相等")
}
测试结果
11-05 10:05:53.655 27299-27299/com.zhqy.javademo E/when: in 1..2
这里可以看出满足了两个条件,但执行了第一个满足的条件,这与Java的switch一致,可以认为when天生自带break.
when可以作为表达式对变量进行赋值,但需要注意的是最后一个判断条件需要使用else,否则kotlin会报错
var z:Int=5;
var result=when(z){
5->Log.e("when","5")
in Int.MIN_VALUE..4->Log.e("when","<5")
else->Log.e("when",">5")
}
测试结果
11-05 10:12:56.396 27646-27646/? E/when: 5
for
kotlin中for循环与java中的for循环有些类似,先看一下Java中的增强型for循环在Kotlin中是如何使用的
var x= listOf<Int>(1,2,3,4)
for (num in x){
Log.e("for","num:$num")
}
测试结果
11-05 09:51:43.492 25575-25575/? E/for: num:1
11-05 09:51:43.492 25575-25575/? E/for: num:2
11-05 09:51:43.492 25575-25575/? E/for: num:3
11-05 09:51:43.492 25575-25575/? E/for: num:4
kotlin中的标准for语句
for(num in 9 downTo 1 ){
Log.e("for","$num")
}
测试结果
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 9
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 8
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 7
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 6
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 5
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 4
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 3
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 2
11-05 10:18:42.145 28386-28386/com.zhqy.javademo E/for: 1
kotlin 中的for循环还能够决定步长
for(num in 9 downTo 1 step 3 ){
Log.e("for","$num")
}
测试结果
11-05 10:20:14.687 28701-28701/com.zhqy.javademo E/for: 9
11-05 10:20:14.687 28701-28701/com.zhqy.javademo E/for: 6
11-05 10:20:14.687 28701-28701/com.zhqy.javademo E/for: 3
对于遍历集合中的数据可以使用如下析构打印出集合中的key和value
var arr1= arrayOf(7,8,9)
for ((index,value) in arr1.withIndex()){
Log.e("for","$index:$value")
}
测试结果
11-05 10:26:29.176 29277-29277/com.zhqy.javademo E/for: 0:7
11-05 10:26:29.176 29277-29277/com.zhqy.javademo E/for: 1:8
11-05 10:26:29.176 29277-29277/com.zhqy.javademo E/for: 2:9
while
Kotlin中的while循环与Java中的while方法基本相同,但需要注意kotlin中while循环不允许在()中赋值,使用方法如下
var w:Int=5
while (w>3){
Log.e("while","$w")
w--
}
测试结果
11-05 10:32:03.682 29684-29684/com.zhqy.javademo E/while: 5
11-05 10:32:03.682 29684-29684/com.zhqy.javademo E/while: 4
do-while
kotlin中的do-while与Java中的使用方法类似,但()不允许进行赋值操作,使用方法如下
var a:Int=10
do{
Log.e("do-while","$a")
a--
}while (a>0)
测试结果
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 10
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 9
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 8
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 7
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 6
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 5
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 4
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 3
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 2
11-05 10:35:38.421 29834-29834/com.zhqy.javademo E/do-while: 1
break
break用于终止最直接包围它的循环。
var arr3= arrayOf(1,2,3,4)
var arr4= arrayOf(1,2,3,4)
for(first in arr3){
for (second in arr4){
if (first==2) break
Log.e("break","$first:$second")
}
}
测试结果
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 1:1
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 1:2
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 1:3
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 1:4
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 3:1
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 3:2
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 3:3
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 3:4
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 4:1
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 4:2
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 4:3
11-05 10:40:57.875 30112-30112/com.zhqy.javademo E/break: 4:4
continue
continue用于继续下一次最直接包围它的循环
var arr3= arrayOf(1,2,3,4)
var arr4= arrayOf(1,2,3,4)
for(first in arr3){
for (second in arr4){
if (first==2) continue
Log.e("break","$first:$second")
}
}
测试结果
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 1:1
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 1:2
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 1:3
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 1:4
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 3:1
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 3:2
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 3:3
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 3:4
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 4:1
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 4:2
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 4:3
11-05 10:44:21.674 32174-32174/com.zhqy.javademo E/break: 4:4
@标签
像break和continue只能跳出最内层的循环或继续最内层的循环,那么如何指定跳出的循环层呢?kotlin使用了@标签来解决这个问题.
var arr3= arrayOf(1,2,3,4)
var arr4= arrayOf(1,2,3,4)
loop@ for(first in arr3){
for (second in arr4){
if (first==2) break@loop
Log.e("break","$first:$second")
}
}
测试结果
11-05 10:51:09.019 32545-32545/com.zhqy.javademo E/break: 1:1
11-05 10:51:09.019 32545-32545/com.zhqy.javademo E/break: 1:2
11-05 10:51:09.019 32545-32545/com.zhqy.javademo E/break: 1:3
11-05 10:51:09.019 32545-32545/com.zhqy.javademo E/break: 1:4
可以看到在最外层循环中打上loop@后 使用break@loop会跳出loop@标记的循环,实现了跳出任意循环的功能,continue也可以使用如下方法。
var arr3= arrayOf(1,2,3,4)
var arr4= arrayOf(1,2,3,4)
loop@ for(first in arr3){
for (second in arr4){
if (first==2) continue@loop
Log.e("break","$first:$second")
}
}
测试结果
11-05 10:54:41.155 556-556/com.zhqy.javademo E/break: 1:1
11-05 10:54:41.155 556-556/com.zhqy.javademo E/break: 1:2
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 1:3
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 1:4
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 3:1
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 3:2
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 3:3
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 3:4
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 4:1
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 4:2
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 4:3
11-05 10:54:41.156 556-556/com.zhqy.javademo E/break: 4:4
@标签也可以使用在lambda表达式式中,我们知道lambda表达式是不支持使用return关键字的,但使用@标签后就可以使用return关键字
fun sum(x:Int,y:Int,action:(x:Int,y:Int)->Int):Int{
return action(x,y)
}
var add= sum(1,2){
x,y->return@sum x+y
}
Log.e("@sum","$add")
测试结果
11-05 11:04:15.365 1167-1167/com.zhqy.javademo E/@sum: 3
@标签还可以用在foreach函数内充当break和continue的作用
list.forEach {
println(it)
}
他的Java字节码为
Iterable $receiver$iv = (Iterable)list;
Iterator var1 = $receiver$iv.iterator();
while(var1.hasNext()) {
Object element$iv = var1.next();
int it = ((Number)element$iv).intValue();
System.out.println(it);
}
也就是使用迭代器遍历整个集合并打印集合数据
利用@xxx实现forEach的conitnue效果
//模仿continue
arr3.forEach loop@{
if (it==2){
return@loop
}
Log.e("foreach","$it")
}
测试结果
11-05 11:15:01.193 2312-2312/com.zhqy.javademo E/foreach: 1
11-05 11:15:01.193 2312-2312/com.zhqy.javademo E/foreach: 3
11-05 11:15:01.193 2312-2312/com.zhqy.javademo E/foreach: 4
Java字节码如下
Object[] $receiver$iv = arr3;
int var1 = $receiver$iv.length;
for(int var2 = 0; var2 < var1; ++var2) {
Object element$iv = $receiver$iv[var2];
int it = ((Number)element$iv).intValue();
if(it != 2) {
Log.e("foreach", "" + it);
}
}
从字节码中可以看出当值为2时则不再打印,实现了类似continue的效果
利用@xxx实现forEach的break效果
//模仿break
run loop@{
arr3.forEach {
if (it==2){
return@loop
}
Log.e("foreach","$it")
}
}
测试结果
11-05 11:21:14.631 3928-3928/com.zhqy.javademo E/foreach: 1
从结果上看确实与break实现了相同的功能
Object[] $receiver$iv = arr3;
int var1 = $receiver$iv.length;
int var2 = 0;
while(var2 < var1) {
Object element$iv = $receiver$iv[var2];
int it = ((Number)element$iv).intValue();
if(it != 2) {
Log.e("foreach", "" + it);
++var2;
continue;
}
}
需要注意的是,run方法只会执行一次,当返回到run时,run不会再次执行,所以在实现的功能上与使用break类似。
网友评论