I still think "traditional" way of using the for loop is very powerful with full control of the index. Why was it removed in Kotlin?

what should I do in kotlin with following java code

for (int i = 0; i < n - 1; i++) {
   for (int j = i + 1; j < n; j++) {
   ....

推荐答案

The answer is: because they decided to remove it. You can still use this syntax:

for (a in 1..10) print("$a ")              // >>> 1 2 3 4 5 6 7 8 9 10

for (a in 10 downTo 1 step 2) print("$a ") // >>> 10 8 6 4 2 

For more info: Ranges & Loops

Kotlin相关问答推荐

Kotlin-删除按钮周围的空格

为什么 Kotlin 在将协变类型参数转换为不变类型参数时会产生 Unchecked Cast 警告?

mutableStateOf 和 mutableStateListOf 有什么区别?

Kotlin 函数有 2 个参数用于对 Map 或 List 进行排序

在 Kotlin 中,如何绑定扩展方法以在接收器范围函数中工作

如何在 Android Jetpack Compose 中的画布上绘制一侧加厚的描边?

如何缩短 MaterialTheme.colors.primary?

is return inside function definition 也是 kotlin 中的表达式

java - 如何将函数作为参数从java传递给kotlin方法?

SpringBoot 2.5.0 对于 Jackson Kotlin 类的支持,请在类路径中添加com.fasterxml.jackson.module: jackson-module-kotlin

Android数据绑定在自定义视图中注入ViewModel

Kotlin 类的调用方法

Kotlin 具体化的泛型不会按计划保持类型

Kotlin中的测试无法访问受保护(protected)的方法

在Kotlin中传递并使用函数作为构造函数参数

Kotlin协程处理错误和实现

如何将map函数应用于Kotlin中的数组并更改其值?

如果作为 RxJava Observable 提供,Kotlin 密封类子类需要强制转换为基类

判断EditText是否为空kotlin android

如何在 Gradle Kotlin DSL 中使用来自 gradle.properties 的插件版本?