如果我有一个循环循环,一旦语句满足,我想打破主循环,我该怎么做?如何从双/嵌套循环中的主/外循环中断开?
这是我的代码:
for(int d = 0; d < amountOfNeighbors; d++){
for(int c = 0; c < myArray.size(); c++){
if(graph.isEdge(listOfNeighbors.get(d), c)){
if(keyFromValue(c).equals(goalWord)){ // once this is true I want to break main loop.
System.out.println("We got to GOAL! It is "+ keyFromValue(c));
break; // this breaks second loop not main one.
}
}
}
}
使用标签。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html –