我是Kotlin的新人,请帮助我实现这个目标.
int number[] = {5,4,1,3,15}
for(int i = number.length; i > 0; i--)
{
Log.e("number", number[i])
}
Run Code Online (Sandbox Code Playgroud)
Nil*_*hod 26
试试这个
for
循环的语法Kotlin
是:
for (item in collection) {
// body of loop
}
Run Code Online (Sandbox Code Playgroud)
身体
for (item: Int in ints) {
// body of loop
}
Run Code Online (Sandbox Code Playgroud)
示例代码
for (i in 0..5) {
println(i) // 0,1,2,3,4,5 --> upto 5
}
Run Code Online (Sandbox Code Playgroud)
for array in array
for (i in 0 until 5) {
println(i) // 0,1,2,3,4 --> upto 4
}
Run Code Online (Sandbox Code Playgroud)
迭代一个带索引的数组.
var arr = arrayOf("neel", "nilu", "nilesh", "nil")
for (item in arr)
{
println(item)
}
Run Code Online (Sandbox Code Playgroud)
了解更多信息 for loop in Kotlin
Int*_*iya 10
读Control Flow Structure in Kotlin
.
for(收藏中的项目)print(item)
for
循环遍历提供迭代器的任何东西.这相当于foreach
循环.
身体可以是一个块.
for (item: Int in ints) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
试试吧
val number = arrayOf(5, 4, 1, 3, 15)
for (i in 0 until number.size)
{
Log.e("NUMBER", number[i].toString())
}
Run Code Online (Sandbox Code Playgroud)
For 循环
for loop
迭代任何提供迭代器的东西。这相当于 C# 等语言中的 foreach 循环。语法如下:
for (item in collection) print(item)
Run Code Online (Sandbox Code Playgroud)
主体可以是块。
for (item: Int in ints) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
如前所述, for 遍历任何提供迭代器的东西,即
具有成员函数或扩展函数 iterator(),其返回类型具有成员函数或扩展函数 next(),并且具有返回布尔值的成员函数或扩展函数 hasNext()。所有这三个函数都需要标记为运算符。
数组上的 for 循环被编译为不创建迭代器对象的基于索引的循环。
如果要遍历带有索引的数组或列表,可以这样做:
for (i in array.indices) {
print(array[i])
}
Run Code Online (Sandbox Code Playgroud)
请注意,这种“范围内的迭代”被编译为最佳实现,而不会创建额外的对象。
或者,您可以使用 withIndex 库函数:
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅链接
归档时间: |
|
查看次数: |
14913 次 |
最近记录: |