<分区>
在某处浏览 java 优秀文章时,我发现这样的代码可以完美编译。
public int myMethod(){
http://www.google.com
return 1;
}
描述说 http:
词将被视为标签和 //www.google.com
被视为评论
我不明白 Java Label 在外部循环中有何用处? 什么情况下应该使用Java Label外循环?
标签 java
<分区>
在某处浏览 java 优秀文章时,我发现这样的代码可以完美编译。
public int myMethod(){
http://www.google.com
return 1;
}
描述说 http:
词将被视为标签和 //www.google.com
被视为评论
我不明白 Java Label 在外部循环中有何用处? 什么情况下应该使用Java Label外循环?
最佳答案
这是在 Java 中使用标签的一个好处:
block:
{
// some code
if(condition) break block;
// rest of code that won't be executed if condition is true
}
嵌套循环的另一种用法:
outterLoop: for(int i = 0; i < 10; i++)
{
while(condition)
{
// some code
if(someConditon) break outterLoop; // break the for-loop
if(anotherConditon) break; // break the while-loop
// another code
}
// more code
}
或:
outterLoop: for(int i = 0; i < 10; i++)
{
while(condition)
{
// some code
if(someConditon) continue outterLoop; // go to the next iteration of the for-loop
if(anotherConditon) continue; // go to the next iteration of the while-loop
// another code
}
// more code
}
关于Java 标签用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19836549/