//循環語句
fun main(args:String){
for(arg in args){//循環1
println(arg)
}
//循環2
for ((index,value) in args.withIndex()){
println("$index -> $value")
}
//循環2等價與
for (indexValue in args.withIndex()){
println("${indexValue.index}->${indexValue.value}")
}
//public data class IndexedValue<out T>(public val index: Int, public val value: T) 這一種類型 可以轉換為(i,v)形式 因為data
//變量值可以替換
for ((i,v) in args.withIndex()){
println("$i -> $v")
}
//自定義類型疊代
val list = MyIntList()
list.add(1)
list.add(2)
list.add(3)
for(i in list){//有疊代器的都可以使用for循環
println(i)
}
var x = 5
while (x > 0){
if(x == 5){
break//跳出循環
}
if(x == 3){
continue//跳過循環
}
println(x)
x--
}
do{
println(x)
x--
}while (x > 0)
}
class MyIterator(val iterator:Iterator<Int>){
operator fun next():Int{
return iterator.next()
}
operator fun hasNext():Boolean{
return iterator.hasNext()
}
}
class MyIntList{
private val list = ArrayList<Int>()
fun add(int:Int){
list.add(int)
}
fun remove(int:Int){
list.remove(int)
}
operator fun iterator():MyIterator{
return MyIterator(list.iterator())
}
}