Kotlin-for循环
2017-11-29 本文已影响0人
122604
虽然使用了collections的函数操作符之后不会再过多地使用for循环,但是for循环在一些情况下仍然很有用。提供一个迭代器它可以作用在任何东西上面:
for (item in collection) {
print(item)
}
如果需要使用index的迭代,也可以使用 ranges (..):
for (index in 0..viewGroup.getChildCount() - 1) {
val view = viewGroup.getChildAt(index)
view.visibility = View.VISIBLE
}
在迭代一个array或者list,index可以用来获取到指定的对象,所以上面的方式不是必要的:
for (index in array.indices){
print(array[index])
}