inner break
An unlabeled break
statement terminates the innermost switch
, for
, while
, or do-while
statement, but a labeled break
terminates an outer statement. The following program, BreakWithLabelDemo
, is similar to the previous program, but uses nested for
loops to search for a value in a two-dimensional array. When the value is found, a labeled break
terminates the outer for
loop (labeled "search"):
查找数组中某个数的序号(序号从0开始排)
- 一维数组
//input
class breakDemo{
public static void main(String[] args) {
int[] arrayInt = { 32, 87, 3, 589,
12, 1076, 2000,
8, 622, 127 };
int searchNumber = 1270;
boolean numberFound = false;
int searchIndex;
//search number's index and return
for (searchIndex = 0; searchIndex < arrayInt.length ;searchIndex++){
if(arrayInt[searchIndex] == searchNumber){
numberFound = true;
break;
}
}
//print the searchNumber and its index
if (numberFound) {
System.out.println("Searched number " + searchNumber + " at index " + searchIndex);
}else{
System.out.println("the number " +searchNumber + " is not found");
}
}
}
//output
the number 1270 is not found
- 二维数组,这里用循环。不过一般MATLAB和python都直接对数组进行操作,java应该也有类似的库的。
注意加label,break;意思是跳出那一整个label的循环,没加label就会循环到结束
break with label
2.1. 正确例子,label把双层循环包起来,break label;
就是跳出一整个循环
//input
class breakDemoTwoDimention {
public static void main(String[] args) {
int[][] arrayInts = {
{32, 87, 3, 589},
{12, 1076, 2000, 8 },
{622, 127, 77, 955}
};
int searchNumber = 12;
int i,j=0;
boolean numberFound = false;
search:
for (i=0; i<arrayInts.length;i++){
for(j=0;j<arrayInts[i].length;j++){
if(arrayInts[i][j] == searchNumber){
numberFound = true;
break search; //break this label "search"
}
}
}
System.out.println(i);
System.out.println(j);
System.out.println(numberFound);
}
}
//correct output
1
0
true
break without label
2.2. 错误例子,break只能跳出if
圈,完全不能影响两个for循环。所以i,j是整个二维数组的行和列。
//input
class breakDemoTwoDimentionWrong {
public static void main(String[] args) {
int[][] arrayInts = {
{32, 87, 3, 589},
{12, 1076, 2000, 8 },
{622, 127, 77, 955}
};
int searchNumber = 12;
int i,j=0;
boolean numberFound = false;
for (i=0; i<arrayInts.length;i++){
for(j=0;j<arrayInts[i].length;j++){
if(arrayInts[i][j] == searchNumber){
numberFound = true;
break;
}
}
}
System.out.println(i);
System.out.println(j);
System.out.println(numberFound);
}
}
//incorrect output
3
4
true
The continue Statement
全称是继续下一个循环,continue 后,忽略以下操作,然后进入下一个for循环。
//input
class ContinueDemo {
public static void main(String[] args) {
String searchSentence = "peter piper picked a peck of pickled peppers";
char searchLetter = 'p';
int letterCounts = 0;
for (int searchIndex = 0;searchIndex<searchSentence.length();searchIndex++ ){
if (searchSentence.charAt(searchIndex) != searchLetter)
continue;
letterCounts++;
}
System.out.println("the sentence contains " + letterCounts + " " + searchLetter);
}
}
//output
the sentence contains 9 p
continue and break mixed up
查找字符串中是否包含一个小的字符串。
class ContinueWithLabelDemo {
public static void main(String[] args) {
String searchSent = "Look for a substring in me";
String searchSub = "sub";
boolean foundString = false;
int searchStep = searchSent.length() - searchSub.length();
test:
for (int searchIndex = 0; searchIndex<searchStep;searchIndex++){
int subSteps = searchSub.length();
int senIndex = searchIndex;
int subIndex = 0;
while (subSteps-- !=0){
if ((searchSent.charAt(senIndex++)) != (searchSub.charAt(subIndex++)))
continue test;
}
foundString = true;
break test;
}
System.out.println(foundString);
}
}
2018.6.22