Kotlin 具有非常好的迭代函数,例如 forEach
或 repeat
,但我无法突破并继续
操作员使用它们
(本地和非本地):
repeat(5) {
break
}
(1..5).forEach {
continue@forEach
}
目标是尽可能接近函数语法来模仿通常的循环。这在一些旧版本的静态编程语言中肯定是可能的,但我很难重现语法。
问题可能是标签(M12)的错误,但我认为第一个示例无论如何都应该可以工作。
在我看来,我在某处读到过一个关于特殊技巧/注释的内容,但我找不到任何关于这个主题的参考资料。可能如下所示:
public inline fun repeat(times: Int, @loop body: (Int) -> Unit) {
for (index in 0..times - 1) {
body(index)
}
}
可以使用以下方法实现中断:
//Will produce "12 done with nested loop"
//Using "run" and a tag will prevent the loop from running again.
//Using return@forEach if I>=3 may look simpler, but it will keep running the loop and checking if i>=3 for values >=3 which is a waste of time.
fun foo() {
run loop@{
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@loop // non-local return from the lambda passed to run
print(it)
}
}
print(" done with nested loop")
}
并且可以通过以下方式实现继续:
//Will produce: "1245 done with implicit label"
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@forEach // local return to the caller of the lambda, i.e. the forEach loop
print(it)
}
print(" done with implicit label")
}
正如这里的任何人所推荐的...阅读文档: Phttps://kotlinlang.org/docs/reference/returns.html#return-at-labels
编辑:虽然主要问题问的是foreach,但重要的是要考虑好的旧“for”。使用静态编程语言并不意味着我们需要一直使用foreach。使用好的旧“for”是完全可以的,有时甚至比foreach更具表现力和简洁:
fun foo() {
for(x in listOf(1, 2, 3, 4, 5){
if (x == 3) break //or continue
print(x)
}
print("done with the good old for")
}
编辑:
根据 Kotlin 的文档,可以模拟继续
使用注释。
fun foo() {
listOf(1, 2, 3, 4, 5).forEach lit@ {
if (it == 3) return@lit // local return to the caller of the lambda, i.e. the forEach loop
print(it)
}
print(" done with explicit label")
}
如果您想模拟< code>break,只需添加一个< code>run块
fun foo() {
run lit@ {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@lit // local return to the caller of the lambda, i.e. the forEach loop
print(it)
}
print(" done with explicit label")
}
}
原始答案:
由于您提供了(Int)-
您有几种选择:
使用常规for循环:
for (index in 0 until times) {
// your code here
}
如果循环是方法中的最后一个代码,您可以使用< code>return
退出方法(如果不是< code>unit方法,则使用< code>return value)。
使用方法< br >创建返回< code>Boolean的自定义重复方法方法以继续。
public inline fun repeatUntil(times: Int, body: (Int) -> Boolean) {
for (index in 0 until times) {
if (!body(index)) break
}
}
这将打印1到5。<代码>return@forEach的作用类似于Java中的关键字continue
,这意味着在这种情况下,它仍然执行每个循环,但如果值大于5,则跳到下一次迭代。
fun main(args: Array<String>) {
val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
nums.forEach {
if (it > 5) return@forEach
println(it)
}
}
这将打印 1 到 10,但跳过 5。
fun main(args: Array<String>) {
val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
nums.forEach {
if (it == 5) return@forEach
println(it)
}
}
这将打印1到4,并在到达5时中断。
fun main(args: Array<String>) {
val nums = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
run breaking@ {
nums.forEach {
if (it == 5) return@breaking
println(it)
}
}
}
链接到ashuges的代码段。
我正在尝试用Java编写下面用Kotlin编写的代码。我无法创建DefaultElementsAdapter,因为我无法获得正确的语法。 我无法获得正确的Java代码 Kotlin班是这样的 我正在尝试使用图书馆https://github.com/m7mdra/HtmlRecycler
如何在 Kotlin 中制作 2D Int 数组?我正在尝试将此代码转换为 Kotlin: 这是我尝试过的一个尝试,第一个2D阵列不起作用,但我让1D阵列起作用了:
正如标题所说,我正在尝试将Java和Kotlin混合在一个项目中。这里有一个很好的例子。混合java kotlin hello world。除了kotlin在src/main/Java/somepackage/SomeClass中找不到我的任何Java类之外,所有的东西都正常工作。Java语言 这是什么原因? 我的身材。gradle看起来像这样 而不是开始尝试在更大的项目上实现这一点。我试图通过创
我试图用OkHttp和Cucumber在静态编程语言中设置一个Spring启动项目,并且在运行Cucumber任务时遇到以下错误。如何修复? 还有build gradle kts片段 我看到了这个错误https://github.com/square/okio/issues/647看起来可能是它,并修复了这个build.gradle,我如何将其翻译为kotlinbuild.gradle.kts?
暴露0.27.1是否能够翻译以下SQL语句? 下面是我尝试的内容,但不幸的是,子查询独立于查询的其余部分工作。 此外,如果可能的话,那么如何使用别名从ResultRow获取结果?在这个示例之后,解决方案似乎是将整个子查询存储在单个变量中,并使用一个alias()方法调用,但这看起来很难看。有没有更好的方法?
在Kotlin中,我覆盖了这两个Google登录功能: 检查与谷歌的连接是否失败。 问题是,有时当我关闭包含用户帐户的对话框时,该对话框会在活动启动时弹出 像这样: 我得到了一个带有以下logcat的<code>IllegalArgumentException</code> E/AndroidRuntime:致命异常:主进程:com.dancam.subscriptions,PID:6346 Ja