/* In this example the labelled break statement
* terminates the outermost loop.
*/
public static void main(String[] args) {
int[][] numbers = {
{1,2,3,4,5},
{10,20,30,40,50},
{100,200,300,400,500}
};
boolean foundIt = false;
int searchFor = 30;
int i = 0;
int j = 0;
search: // This is the label for the outer loop.
for (i = 0; i < numbers.length; i++) {
for (j = 0; j < numbers[i].length; j++) {
if (numbers[i][j] == searchFor) {
foundIt = true;
break search;
}
}
}
if (foundIt) {
System.out.println(searchFor + " found at position " + i + ":" + j);
} else {
System.out.println(searchFor + " could not be found.");
}
}
}
No comments:
Post a Comment