为什么我无法在Java 8中继续使用带有Collections#forEach的标签?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了为什么我无法在Java 8中继续使用带有Collections#forEach的标签?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1561字,纯文字阅读大概需要3分钟。
内容图文
![为什么我无法在Java 8中继续使用带有Collections#forEach的标签?](/upload/InfoBanner/zyjiaocheng/729/2b9ee56090f444d88a689f33ad8d38d2.jpg)
编译后没有任何错误:
class App {
boolean b;
boolean c;
void foo(List<Integer> ints) {
myLabel:
for (Integer i : ints) {
while (!b) {
if (c) {
continue myLabel;
}
}
};
}
}
但如果我按如下方式修改foo:
void foo(List<Integer> ints) {
myLabel:
ints.forEach(integer -> {
while (!b) {
if (c) {
continue myLabel;
}
}
});
}
我得到错误:(17,21)undefined label:myLabel
有什么不同?据我所知,新的forEach只是增强for循环的捷径?
解决方法:
正如评论中所述,forEach只是一个方法调用.片段
myLabel: ints.forEach(integer -> ...);
identifier statement labels are used with
break
orcontinue
statements (§14.15, §14.16) appearing anywhere within the labeled statement.
重复一下,带标签的语句是方法调用表达式.您的继续声明不在标签声明范围内.
你的continue statement是在lambda表达式体内出现的while语句中.
A
continue
statement with labelIdentifier
attempts to transfer
control to the enclosing labeled statement (§14.7) that has the same
Identifier
as its label; that statement, which is called the continue
target, then immediately ends the current iteration and begins a new
one.[…]
The continue target must be a
while
,do
, orfor
statement, or a compile-time error occurs.A
continue
statement must refer to a label within the immediately
enclosing method, constructor, initializer, or lambda body. There are
no non-local jumps. If no labeled statement withIdentifier
as its
label in the immediately enclosing method, constructor, initializer,
or lambda body contains the continue statement, a compile-time error
occurs.
由于在紧邻的lambda体中没有名为myLabel的标记(while,do或for)语句,因此会出现编译时错误.
内容总结
以上是互联网集市为您收集整理的为什么我无法在Java 8中继续使用带有Collections#forEach的标签?全部内容,希望文章能够帮你解决为什么我无法在Java 8中继续使用带有Collections#forEach的标签?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。