//循环语句
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())
}
}