Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5409ac0

Browse files
committed
从一个多层嵌套循环中直接退出
1 parent 85639e3 commit 5409ac0

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

‎README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ stackoverflow-Java-top-qa
1515
* [如何遍历map对象](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/iterate-through-a-hashmap.md)
1616
* [public,protected,private,不加修饰符。有什么区别呢?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md)
1717
* [如何判断数组Array是否包含指定的值?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/in-java-whats-the-difference-between-public-default-protected-and-private.md)
18+
* [重写(Override)equlas和hashCode方法时应考虑的问题](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java.md)
19+
* [从一个多层嵌套循环中直接退出](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/breaking-out-of-nested-loops-in-java.md)
20+
1821

1922
> 代码规范
2023
@@ -42,13 +45,9 @@ stackoverflow-Java-top-qa
4245
- [Converting String to int in Java?](http://stackoverflow.com/questions/5585779/converting-string-to-int-in-java)
4346
- [Is there a unique Android device ID?](http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id)
4447
- [How to test a class that has private methods, fields or inner classes](http://stackoverflow.com/questions/34571/how-to-test-a-class-that-has-private-methods-fields-or-inner-classes)
45-
-
46-
4748
- [Why does this code using random strings print "hello world"?](http://stackoverflow.com/questions/15182496/why-does-this-code-using-random-strings-print-hello-world)
48-
-
4949
- [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven)
5050
- [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files)
51-
-
5251
- [In Java, how can I test if an Array contains a certain value?](http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value)
5352
- [Why is executing Java code in comments with certain Unicode characters allowed?](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed)
5453
- [Dealing with "java.lang.OutOfMemoryError: PermGen space" error](http://stackoverflow.com/questions/88235/dealing-with-java-lang-outofmemoryerror-permgen-space-error)
@@ -58,7 +57,6 @@ stackoverflow-Java-top-qa
5857
- [Android SDK installation doesn't find JDK](http://stackoverflow.com/questions/4382178/android-sdk-installation-doesnt-find-jdk)
5958
- [How do I call one constructor from another in Java?](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java)
6059
- [Initialization of an ArrayList in one line](http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line)
61-
- [Breaking out of nested loops in Java](http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java)
6260
- [Java inner class and static nested class](http://stackoverflow.com/questions/70324/java-inner-class-and-static-nested-class)
6361
- ['Must Override a Superclass Method' Errors after importing a project into Eclipse](http://stackoverflow.com/questions/1678122/must-override-a-superclass-method-errors-after-importing-a-project-into-eclips)
6462
- [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)
@@ -76,7 +74,10 @@ stackoverflow-Java-top-qa
7674
- [How to concatenate two arrays in Java?](http://stackoverflow.com/questions/80476/how-to-concatenate-two-arrays-in-java)
7775
- [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array)
7876
- [Why can't I switch on a String?](http://stackoverflow.com/questions/338206/why-cant-i-switch-on-a-string)
79-
- [What issues should be considered when overriding equals and hashCode in Java?](http://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java)
77+
- [What issues should be considered when overriding equals and
78+
-
79+
-
80+
8081
- [How to create a Java String from the contents of a file?](http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file)
8182
- [Why does Java have transient variables?](http://stackoverflow.com/questions/910374/why-does-java-have-transient-variables)
8283
- [How can I convert a stack trace to a string?](http://stackoverflow.com/questions/1149703/how-can-i-convert-a-stack-trace-to-a-string)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
##从一个多层嵌套循环中直接退出
2+
3+
###问题
4+
Java中如何从一个多层嵌套循环中退出,例如下面,有两个循环,break只能退出一个for循环,不能直接跳过第二个for循环
5+
```java
6+
for (Type type : types) {
7+
for (Type t : types2) {
8+
if (some condition) {
9+
// Do something and break...
10+
break; // 这样只退出了最里的for循环
11+
}
12+
}}
13+
```
14+
15+
###回答
16+
17+
可以用brea+label的语法,例子如下
18+
```java
19+
public class Test {
20+
public static void main(String[] args) {
21+
outerloop:
22+
for (int i=0; i < 5; i++) {
23+
for (int j=0; j < 5; j++) {
24+
if (i * j > 6) {
25+
System.out.println("Breaking");
26+
break outerloop;
27+
}
28+
System.out.println(i + " " + j);
29+
}
30+
}
31+
System.out.println("Done");
32+
}
33+
}
34+
```
35+
36+
首先在for循环前加标签,如例子中的outerloop,然后在for循环内break label(如本例的outerloop),就会跳出该label指定的for循环。
37+
38+
stackoverflow链接:
39+
http://stackoverflow.com/questions/886955/breaking-out-of-nested-loops-in-java

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /