Java Reference
In-Depth Information
break target
, then immediately completes normally. In this case, the break target need not
be a
switch
,
while
,
do
, or
for
statement.
To be precise, a
break
statement with label
Identifier
always completes abruptly, the reason
being a
break
with label
Identifier
.
A
break
statement must refer to a label within the immediately enclosing method, construct-
or, or initializer. There are no non-local jumps. If no labeled statement with
Identifier
as
its label in the immediately enclosing method, constructor, or initializer contains the
break
statement, a compile-time error occurs.
It can be seen, then, that a
break
statement always completes abruptly.
The preceding descriptions say “attempts to transfer control” rather than just “trans-
whose
try
blocks or
catch
clauses contain the
break
statement, then any
finally
clauses
of those
try
statements are executed, in order, innermost to outermost, before control
is transferred to the break target. Abrupt completion of a
finally
clause can disrupt the
transfer of control initiated by a
break
statement.
Example 14.15-1. The
break
Statement
In the following example, a mathematical graph is represented by an array of arrays.
A graph consists of a set of nodes and a set of edges; each edge is an arrow that points
from some node to some other node, or from a node to itself. In this example it is as-
sumed that there are no redundant edges; that is, for any two nodes
P
and
Q
, where
Q
may be the same as
P
, there is at most one edge from
P
to
Q
.
Nodes are represented by integers, and there is an edge from node
i
to node
edges[
i
][
j
]
for every
i
and
j
for which the array reference
edges[
i
][
j
]
does not throw an
ArrayIn-
dexOutOfBoundsException
.
The task of the method
loseEdges
, given integers
i
and
j
, is to construct a new graph
by copying a given graph but omitting the edge from node
i
to node
j
, if any, and the
edge from node
j
to node
i
, if any:
class Graph {
int edges[][];
public Graph(int[][] edges) { this.edges = edges; }
public Graph loseEdges(int i, int j) {
int n = edges.length;
int[][] newedges = new int[n][];