我正在编写For-each循环,并在for-each循环中检查条件。如果满足该条件,我想重新启动循环。是否有诸如 continue 或 break 之类的关键字可以从头开始
>>> obj="""
... /API/{id}/one
... /{two}/one/three
... /three/four/{five}
... """
>>> newobj = obj.replace('{','${')
>>> print(newobj)
/API/${id}/one
/${two}/one/three
/three/four/${five}
答案 0 :(得分:3)
您可以在for循环中使用索引,然后在需要重新启动循环时将此索引设置为零。
for (int i = 0; i < ordersItemList.size(); i++) {
PoDetails item = ordersItemList.get(i);
if (nextPosition == incrementPosition) {
if (some condition) {
break;
} else {
if (some condition) {
continue;
}else{
//I want to restart the for-each loop here
i = 0; // set the index to zero here, then it will start the loop from the begnning
}
}
} else {
nextPosition++;
}
}
答案 1 :(得分:1)
OUTER: //outer label
for (PoDetails items : ordersItemList) {
if (nextPosition == incrementPosition) {
if (some condition){
break;
} else{
if (some condition){
continue;
} else{
continue OUTER:; // This will call the loop from OUTER:
}
}
} else {
nextPosition++;
}
}