continue 不能在循环之外使用(实际上不在循环之外)
Posted
技术标签:
【中文标题】continue 不能在循环之外使用(实际上不在循环之外)【英文标题】:continue cannot be used outside of a loop (it isn't outside actually) 【发布时间】:2012-10-31 11:53:54 【问题描述】:我不明白为什么 continue 会在这里导致错误
public void clear()
log.debug("Clearing hash");
// wow!
while( hash.size()>0 )
for(Map.Entry<Node,Node> entry : hash.entrySet())
clearingParents:
while( entry.getKey().ups.size() > 0 )
for(Node node : entry.getKey().ups)
log.debug("Clearing , hash size is ", node, hash.size());
if( node.sizeUps() == 0 )
node.clear();
continue clearingParents;
else
log.debug("was skipped since inserted");
break clearingParents;
我正在使用这个方案,因为 node.clear() 导致迭代器出现损坏
【问题讨论】:
不!!!!这段代码被诅咒了!圈复杂度和标签,你不尊重 Dijkstra 吗?!? 我想为任何发现此问题的人添加此内容:确保您没有在循环内的 lambda 内部工作。要摆脱 lambda,只需使用return;
【参考方案1】:
您标记一个块而不是 while 循环。
你可以在labeled block
中使用break
而不是continue
和labeled block
continue a labeled block
没有意义,因为它不是循环
像下面这样标记你的循环
clearingParents: while( entry.getKey().ups.size() > 0 )
for(Node node : entry.getKey().ups)
log.debug("Clearing , hash size is ", node, hash.size());
if( node.sizeUps() == 0 )
node.clear();
continue clearingParents;
else
log.debug("was skipped since inserted");
break clearingParents;
研究标记循环here
【讨论】:
以上是关于continue 不能在循环之外使用(实际上不在循环之外)的主要内容,如果未能解决你的问题,请参考以下文章