I look at the sample code and found it here

public static List<Word> detectAllWords(int[][] crossword, String... words) { List<Word> wordList = new ArrayList<>(); int[][] searchDirections = new int[][] { {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, }; for (String word : words) nextWord:{ for (int i = 0; i < crossword.length; i++) { for (int j = 0; j < crossword[i].length; j++) { if (word.charAt(0) == crossword[i][j]) for (int directions = 0; directions < searchDirections.length; directions++) { int tmp_i = i, tmp_j = j, wordPos = 1; while (wordPos < word.length()) { tmp_i += searchDirections[directions][0]; tmp_j += searchDirections[directions][1]; if (tmp_i < 0 || tmp_i >= crossword.length || tmp_j < 0 || tmp_j >= crossword[tmp_i].length) break; if (word.charAt(wordPos) != crossword[tmp_i][tmp_j]) break; else if (wordPos == word.length() - 1) { Word tWord = new Word(word); tWord.setStartPoint(j, i); tWord.setEndPoint(tmp_j, tmp_i); wordList.add(tWord); break nextWord; } wordPos++; } } } } } return wordList; } 

So the first time I see this condition

 for (String word : words) nextWord:{ 

and then it's like that tied to the line below

 break nextWord; 

Tell me how it works or where there is something to read?

  • Just go to the label . It is strictly not recommended to use. - pavlofff

1 answer 1

This is a break statement with a label. If it is triggered, a transition occurs to the code that is located behind the code block with the specified label.

It is convenient to use to exit nested loops. The break operator without a label allows you to go up one level only, and using the break label you can immediately exit the loop of any nesting.

Here it is used to exit the nested loop (from the 5th level, the transition immediately to the first level loop occurs).

You can read in any guide to Java.

A simple example:

 public class Main { public static void main(String[] args) { label: for (int i=0; i<4; i++) { for (int j=0; j<4; j++) { System.out.println("i = " + i + ", j = " + j); if (i == 2 && j == 3) { break label; } } } System.out.println("End"); } } 

Output to console:

 i = 0, j = 0 i = 0, j = 1 i = 0, j = 2 i = 0, j = 3 i = 1, j = 0 i = 1, j = 1 i = 1, j = 2 i = 1, j = 3 i = 2, j = 0 i = 2, j = 1 i = 2, j = 2 i = 2, j = 3 End 

Ps. break label should be used only when absolutely necessary, as it complicates code perception.