for 通常
1 2 3 |
for(i in 1..5){ Log.d("MainActivity", "i="+i) } |
1 2 3 4 5 |
D/MainActivity: i=1 D/MainActivity: i=2 D/MainActivity: i=3 D/MainActivity: i=4 D/MainActivity: i=5 |
for 通常 step付き
1 2 3 |
for(i in 1..5 step 2){ Log.d("MainActivity", "i="+i) } |
1 2 3 |
D/MainActivity: i=1 D/MainActivity: i=3 D/MainActivity: i=5 |
for 減少
1 2 3 |
for(i in 5 downTo 1 ){ Log.d("MainActivity", "i="+i) } |
1 2 3 4 5 |
D/MainActivity: i=5 D/MainActivity: i=4 D/MainActivity: i=3 D/MainActivity: i=2 D/MainActivity: i=1 |
for 手前まで
1 2 3 |
for(i in 1 until 5){ Log.d("MainActivity", "until:i="+i) } |
1 2 3 4 |
D/MainActivity: until:i=1 D/MainActivity: until:i=2 D/MainActivity: until:i=3 D/MainActivity: until:i=4 |
for 配列
1 2 3 4 |
var data = arrayOf("A","B","C") for(i in data){ Log.d("MainActivity", "array:i="+i) } |
1 2 3 |
D/MainActivity: array:i=A D/MainActivity: array:i=B D/MainActivity: array:i=C |
for 配列 インデックス
1 2 3 4 |
var data = arrayOf("A","B","C") for(i in data.indices){ Log.d("MainActivity", "array:i="+i) } |
1 2 3 |
D/MainActivity: array:i=0 D/MainActivity: array:i=1 D/MainActivity: array:i=2 |
forEach
1 2 3 4 |
var b = arrayOf("A", "B", "C") b.forEach { Log.d("MainActivity","b="+it) } |
1 2 3 |
D/MainActivity: b=A D/MainActivity: b=B D/MainActivity: b=C |
forEach インデックス付き
1 2 3 4 |
var b = arrayOf("A", "B", "C") b.forEachIndexed { index, element -> Log.d("MainActivity","b[$index]=$element") } |
1 2 3 |
D/MainActivity: b[0]=A D/MainActivity: b[1]=B D/MainActivity: b[2]=C |
while
1 2 3 4 5 |
var i = 1 while(i<5){ Log.d("MainActivity","i="+i) i++ } |
1 2 3 4 |
D/MainActivity: i=1 D/MainActivity: i=2 D/MainActivity: i=3 D/MainActivity: i=4 |
do while
1 2 3 4 5 |
var i = 1 do{ Log.d("MainActivity","i="+i) i++ }while(i<1) |
1 |
D/MainActivity: i=1 |
repeat
1 2 3 4 5 |
var i = 0 repeat(5){ Log.d("MainActivity","i="+i) i++ } |
1 2 3 4 5 |
D/MainActivity: i=0 D/MainActivity: i=1 D/MainActivity: i=2 D/MainActivity: i=3 D/MainActivity: i=4 |
break(途中で止める)
1 2 3 4 5 6 7 |
for(i in 1..10){ Log.d("MainActivity","befor:i="+i) if( i > 5){ break } Log.d("MainActivity","after:i="+i) } |
1 2 3 4 5 6 7 8 9 10 11 |
D/MainActivity: befor:i=1 D/MainActivity: after:i=1 D/MainActivity: befor:i=2 D/MainActivity: after:i=2 D/MainActivity: befor:i=3 D/MainActivity: after:i=3 D/MainActivity: befor:i=4 D/MainActivity: after:i=4 D/MainActivity: befor:i=5 D/MainActivity: after:i=5 D/MainActivity: befor:i=6 |
※forEachでは利用できない。
continue(後続処理スキップ)
1 2 3 4 5 6 7 |
for(i in 1..10){ Log.d("MainActivity","befor:i="+i) if( i > 5){ continue } Log.d("MainActivity","after:i="+i) } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
D/MainActivity: befor:i=1 D/MainActivity: after:i=1 D/MainActivity: befor:i=2 D/MainActivity: after:i=2 D/MainActivity: befor:i=3 D/MainActivity: after:i=3 D/MainActivity: befor:i=4 D/MainActivity: after:i=4 D/MainActivity: befor:i=5 D/MainActivity: after:i=5 D/MainActivity: befor:i=6 D/MainActivity: befor:i=7 D/MainActivity: befor:i=8 D/MainActivity: befor:i=9 D/MainActivity: befor:i=10 |
※forEachでは利用できない。