有没有一种优雅的方法可以跳过while-loop中的迭代?
我想做的是
while(rs.next())
{
if(f.exists() && !f.isDirectory()){
//then skip the iteration
}
else
{
//proceed
}
}发布于 2013-02-13 22:45:31
continue
while(rs.next())
{
if(f.exists() && !f.isDirectory())
continue; //then skip the iteration
else
{
//proceed
}
}发布于 2013-02-13 22:46:07
既然你可以使用continue,为什么不直接颠倒if中的逻辑呢?
while(rs.next())
{
if(!f.exists() || f.isDirectory()){
//proceed
}
}您甚至不需要else {continue;},因为如果不满足if条件,它将继续运行。
发布于 2013-02-13 22:42:57
尝试在要跳过1次迭代的位置添加continue;。
与break关键字不同,continue不会终止循环。相反,它跳到循环的下一次迭代,并停止执行此迭代中的任何其他语句。这允许我们绕过当前序列中的其余语句,而不会停止循环中的下一次迭代。
http://www.javacoffeebreak.com/articles/loopyjava/index.html
https://stackoverflow.com/questions/14856028
复制相似问题