loops - Referring to "it" in the outer for loop
问题描述
I have a question about accessing the it in the outer loop in kotlin. I am trying to see how many letters differ between two strings. I would like to know is there a way to access the outer for loop with it?
fun compute (stringOne: String, stringTwo: String): Int {
var i = 0
stringOne.toCharArray().forEach @loop{
stringTwo.toCharArray().forEach {
if (it@loop.equals(it))
i++
}
}
return i
}
解决方案
您可以将命名参数用于循环
fun compute (stringOne: String, stringTwo: String): Int {
var i = 0
stringOne.toCharArray().forEach { char1 ->
stringTwo.toCharArray().forEach { char2 ->
if (char1 == char2)
i++
}
}
return i
}
推荐阅读
- adhoc - NS-3,将队列大小添加到 adhoc 网络
- angular - 将第 3 方 JS 插件安装到 Angular 7 中
- php - Can't get bidirectional data from pivot table
- java - Spring boot @TextIndexed 不在 MongoDB 中创建索引
- azure - 当我的 linting 脚本返回错误时,如何让我的 Azure DevOps Pipeline 构建失败?
- linux - 跑步者不健康,将被禁用
- asp.net-core - 如何从 ActionFilter 重定向到 Asp.net Core 2.2 中的另一个动作或路由?
- javascript - 用于 Visual Studio 代码编辑器的 Javascript IntelliSence
- android - 三星 Galaxy S8 导航栏覆盖固定位置 div
- java - 从数组中拉取范围