Iterable
public interface Iterable<out T> {
/**
* Returns an iterator over the elements of this object.
*/
public operator fun iterator(): Iterator<T>
}
Iterator
- next()
- hasNext()
public interface Iterator<out T> {
/**
* Returns the next element in the iteration.
*/
public operator fun next(): T
/**
* Returns `true` if the iteration has more elements.
*/
public operator fun hasNext(): Boolean
}
/**
* Given an [iterator] function constructs an [Iterable] instance that returns values through the [Iterator]
* provided by that function.
*/
@kotlin.internal.InlineOnly
public inline fun <T> Iterable(crossinline iterator: () -> Iterator<T>): Iterable<T> = object : Iterable<T> {
override fun iterator(): Iterator<T> = iterator()
}
fun main(args: Array<String>) {
var x = 5
while(x > 0){
println(x)
x--
}
do{
println(x)
x--
}while (x > 0)
// for (arg in args){
// println(arg)
// }
//
// for((index, value) in args.withIndex()){
// println("$index -> $value")
// }
//
// for(indexedValue in args.withIndex()){
// println("${indexedValue.index} -> ${indexedValue.value}")
// }
//
// val list = MyIntList()
// list.add(1)
// list.add(2)
// list.add(3)
//
// for(i in list){
// println(i)
// }
}
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())
}
}
当前内容版权归 JackChan1999 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 JackChan1999 .