問題:
I've got a nested loop construct like this: 我有一個像這樣的嵌套循環構造: app
for (Type type : types) { for (Type t : types2) { if (some condition) { // Do something and break... break; // Breaks out of the inner loop } } }
Now how can I break out of both loops? 如今如何擺脫兩個循環? I've looked at similar questions, but none concerns Java specifically. 我看過相似的問題,但沒有一個是Java特有的。 I couldn't apply these solutions because most used gotos. 我沒法應用這些解決方案,由於大多數使用的gotos。 oop
I don't want to put the inner loop in a different method. 我不想將內部循環使用其餘方法。 this
I don't want to rerun the loops. 我不想從新運行循環。 When breaking I'm finished with the execution of the loop block. 中斷時,我完成了循環塊的執行。 spa
解決方案:
參考一: https://stackoom.com/question/3ijj/如何突破Java中的嵌套循環參考二: https://oldbug.net/q/3ijj/How-do-I-break-out-of-nested-loops-in-Java