java 如何在java代码中使用标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28381212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:29:00  来源:igfitidea点击:

How to use labels in java code?

javalabel

提问by Param

I am getting error while using break statements in labels in java code. This is showing undefined label. It is wrong to write code like this. Please assist me in using it correctly. Thanks in advance.

在 Java 代码的标签中使用 break 语句时出现错误。这是显示undefined label. 写这样的代码是错误的。请帮助我正确使用它。提前致谢。

 while (true)
          {
            label149: if (!localIterator2.hasNext());
            while (true)
            {
              i++;
              break;
              HashMap localHashMap2 = (HashMap)localIterator2.next();
              if (!((String)localHashMap1.get("name")).equalsIgnoreCase((String)localHashMap2.get("emotion")))
                break label149;
              if (!((String)localHashMap2.get("IS_paid")).equalsIgnoreCase("1"))
                break label246;
              ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "1");
            }
            label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");
          }

回答by RealSkeptic

A breakwith a label is not the same as a gotostatement. Java does not have a gotostatement.

break带有标签的A与goto语句不同。Java 没有goto声明。

A label marks the statement that follows it. You can use it to break out of that statement, and only out of that statement. Control of flow will always transfer to the end of the labeled statement.

标签标记其后的语句。你可以用它来跳出那个陈述,而且只能跳出那个陈述。流程控制将始终转移到标记语句的末尾。

So what do you have here?

那么你这里有什么?

        label149: if (!localIterator2.hasNext());

Because of the semicolon after the if, this is in fact the entire labeled statement. So your break label149is not within its scope at all. If you did not have that semicolon, the ifblock would include the while, and then the break label149would work. But control would be transferred to the line after the end of the whileblock.

由于 if 后面的分号,这实际上是整个标记语句。所以你的break label149根本不在它的范围内。如果您没有那个分号,则该if块将包含while,然后break label149将起作用。但控制将在while块结束后转移到该行。

       label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");

This is the statement marked by label246. Again, the break label246is not inside it, so it is not in its scope, and you can`t break out of a statement you are not inside of.

这是label246标记的语句。同样,the break label246is not in it,所以它不在它的范围内,你不能跳出你不在里面的语句。

回答by SMA

With Break and continue, you could use label where you have complicated loops within your application. But its always advisable to avoid making use of labels as it becomes hard to read the code and resembles a bad design altogether (remember goto (still a keyword in java) in other languages?). An example of label would be like:

使用 Break 和 continue,您可以在应用程序中有复杂循环的地方使用标签。但是始终建议避免使用标签,因为它变得难以阅读代码并且完全类似于糟糕的设计(还记得其他语言中的 goto(仍然是 Java 中的关键字)吗?)。标签示例如下:

abc: for (;;) {
    for (;; i++) {
        if (i == 255) {
            break abc;
        }
    }
}
System.out.println("Noww i am back");

This says that i need to break of loop which i have labeled as abc. So it will come out of outer for loop. If i would have omitted the abc there then it would have come out of the inner most loop i.e. the second loop from which i said it to break from. You can't expect label to be anywhere in the code.

这表示我需要中断我标记为 abc 的循环。所以它会从外部 for 循环中出来。如果我在那里省略了 abc,那么它就会从最里面的循环中出来,即我说它要中断的第二个循环。你不能期望标签出现在代码中的任何地方。

回答by Manjitha teshara

A label break statement terminates the current loop and proceeds to the first statement that follows the loop that is labeled by the identifier that follows the keyword break.

标签 break 语句终止当前循环并继续执行由关键字 break 后的标识符标记的循环后的第一条语句。

Eg:

例如:

     for (int i=0; i<3; i++)
            {
             resume:
                for (……………)
                          {
                                 ……..
                              if (……..) break resume;
                 }
    }