How to write complex Kotlin for loop
编写像
1 | for (byte i = 1 << 7; i != 0; i >>= 1) |
或
1 | for (byte i = 0x01; i != 0; i <<= 1) |
感谢您的帮助。
这不是Kotlin中
1 2 3 4 5 | var i: Int = 1 shl 7 while (i != 0) { // . . . i = i shr 1 } |
如果您不喜欢
1 2 3 4 5 6 7 | generateSequence(1 shl 7) { it shr 1 } .takeWhile { it != 0 } .forEach { ... } // or: generateSequence(1 shl 7) { (it shr 1).takeIf { it != 0 } }.forEach { ... } |
否则,迈克尔斯(Michaels)关于使用