我有一个这样的嵌套循环结构:
for (Type type : types) {
for (Type t : types2) {
if (some condition) {
// Do something and break...
break; // Breaks out of the inner loop
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我怎样才能摆脱两个循环?我看过类似的问题,但没有一个特别关注Java.我无法应用这些解决方案,因为大多数使用了gotos.
我不想把内循环放在不同的方法中.
我不想重新运行循环.当我完成循环块的执行时.
我希望stop=true在内部for循环中达到条件时打破外部while 循环.这可能吗?
urlsToScrape.addAll(startUrls);
boolean stop=false;
while(stop==false){
for(Link url:urlsToScrape){
p.rint(url.depth+" >= "+depth);
if(url.depth>=depth){
p.rint("STOP!");
stop=true;
break;
}
p.rint("scrape(): "+url.link+" / "+url.depth);
processLinks(url.link,url.depth);
urlsToScrape.remove(url);
scrapedUrls.add(url);
}
}
Run Code Online (Sandbox Code Playgroud)