Java继续标签已弃用?

Cos*_*min 17 java continue deprecated

我有2个fors,嵌套后我有一些代码,如果条件在嵌套for中为真,我不想执行.如果我使用break代码将执行,所以(正如我在SCJP中学到的)我用于continue label;外部for.这是Java的弃用用法吗?老式的?有人建议使用递归或其他东西,但对我来说这是完全正常,简单,最新和完美的方式.

here:
for (bla bla) {
   for (bla bla) {
      if (whatever) continue here;
   }
// some code I don't want to execute if whatever is true
}
Run Code Online (Sandbox Code Playgroud)

谢谢

编辑:
如果我将我的问题改为:你怎么能在多个嵌套的fors之间"导航"?这种方法是"推荐"方式吗?因为这就是它在SCJP Book中所说的.如果不是..这将意味着,Katherine SierraBert Bates是错误的?

编辑2:
为什么continue label;气馁?我想要回答OOP或Java的概念或内部工作,可能会出错.

Mik*_*dmo 7

我会重构以使其更具可读性.

例:

if (!checkWhatever()) {
    // some code I don't want to execute if whatever is true
}
boolean checkWhatever() {
    for (bla bla) {
       for (bla bla) {
          if (whatever) return false;
       }
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 没有通用的方法来替换"继续标签".另一方面,总有一些方法来取代它. (7认同)
  • @Cosmin,我会说,没有`Object [] [] [] [] [] []`(除非你有一个5维物理问题.;)在某些时候,使用一个类是有道理的包装`[]`或`[] []`. (7认同)
  • @Cosmin Vacaroiu 5嵌套循环?听起来像设计问题,应该重构. (4认同)

Pet*_*rey 5

我会说它气馁。在替代方案更复杂或更容易出错(即不是改进)的情况下,它仍然具有有效的用途


Mar*_*ope 5

答案是:这取决于.如果您发现自己使用continue了很多,那么可能表明您的代码需要重构.但是,在您给出的场景中,它似乎是一个使用它的好地方.