1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
<html>
<head>
<title>
Handbuch der Java-Programmierung, 5. Auflage
</title>
</head>
<body>
<a name="startofbody"></a>
<script language="JavaScript" src="hjp4lib.js">
</script>
<script language="JavaScript">
installKbdHandler("97,#startofbody;101,#endofbody;116,cover.html;122,k100003.html;115,search.html;105,index.html;100,JDKDOCS;112,APIDOCS;104,k100040.html;106,k100042.html;107,k100044.html;108,k100046.html");
</script>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100040.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100042.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100044.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100046.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">Kapitel 6 - Anweisungen
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id006003"></a>
<h2>6.3 <a name="ixa100406">Schleifen</a></h2>
<hr>
<ul>
<li><a href="k100043.html#sectlevel2id006003">6.3 Schleifen</a>
<ul>
<li><a href="k100043.html#sectlevel3id006003001">6.3.1 Die while-Schleife</a>
<ul>
<li><a href="k100043.html#sectlevel4id006003001001">Syntax</a>
<li><a href="k100043.html#sectlevel4id006003001002">Bedeutung</a>
</ul>
<li><a href="k100043.html#sectlevel3id006003002">6.3.2 Die do-Schleife</a>
<ul>
<li><a href="k100043.html#sectlevel4id006003002001">Syntax</a>
<li><a href="k100043.html#sectlevel4id006003002002">Bedeutung</a>
</ul>
<li><a href="k100043.html#dieforschleife">6.3.3 Die for-Schleife</a>
<ul>
<li><a href="k100043.html#sectlevel4id006003003001">Syntax</a>
<li><a href="k100043.html#sectlevel4id006003003002">Bedeutung</a>
<li><a href="k100043.html#sectlevel4id006003003003">break und continue</a>
<li><a href="k100043.html#sectlevel4id006003003004">Die erweiterte for-Schleife</a>
</ul>
</ul>
</ul>
<hr>
<p>
Java besitzt die drei üblichen Schleifenanweisungen prozeduraler
Programmiersprachen: eine nichtabweisende, eine abweisende und eine
Zählschleife. Die Syntax und Semantik der Schleifen ist fast
vollständig identisch zu den entsprechenden Anweisungen in C.
<!-- Section -->
<a name="sectlevel3id006003001"></a>
<h3>6.3.1 Die while-Schleife<a name="ixa100407"></a> </h3>
<!-- Section -->
<a name="sectlevel4id006003001001"></a>
<h4>Syntax </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
while (ausdruck)
anweisung;
</pre>
</font>
</td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel4id006003001002"></a>
<h4>Bedeutung </h4>
<p>
Zuerst wird der Testausdruck, der vom Typ <a href="index_b.html#ixb100072"><font color=#000080><tt>boolean</tt></font></a>
sein muss, geprüft. Ist er <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>,
wird die Anweisung ausgeführt, andernfalls wird mit der ersten
Anweisung hinter der Schleife weitergemacht. Nachdem die Anweisung
ausgeführt wurde, wird der Testausdruck erneut geprüft usw.
Die Schleife wird beendet, sobald der Test <a href="index_f.html#ixb100234"><font color=#000080><tt>false</tt></font></a>
ergibt.
<!-- Section -->
<a name="sectlevel3id006003002"></a>
<h3>6.3.2 Die do-Schleife<a name="ixa100408"></a> </h3>
<!-- Section -->
<a name="sectlevel4id006003002001"></a>
<h4>Syntax </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
do
anweisung;
while (ausdruck);
</pre>
</font>
</td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel4id006003002002"></a>
<h4>Bedeutung </h4>
<p>
Die <a href="index_d.html#ixb100077"><font color=#000080><tt>do</tt></font></a>-Schleife
arbeitet <i>nichtabweisend</i>, d.h. sie wird mindestens einmal ausgeführt.
Da zunächst die Schleifenanweisung ausgeführt und erst dann
der Testausdruck überprüft wird, kann die <a href="index_d.html#ixb100077"><font color=#000080><tt>do</tt></font></a>-Schleife
frühestens nach einem Durchlauf regulär beendet werden.
Die Bearbeitung der Schleife wird immer dann beendet, wenn der Test
des Schleifenausdrucks <a href="index_f.html#ixb100234"><font color=#000080><tt>false</tt></font></a>
ergibt.
<!-- Section -->
<a name="dieforschleife"></a>
<h3>6.3.3 Die <a name="ixa100409">for-Schleife</a> </h3>
<!-- Section -->
<a name="sectlevel4id006003003001"></a>
<h4>Syntax </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
for (init; test; update)
anweisung;
</pre>
</font>
</td>
</tr>
</table>
<!-- Section -->
<a name="sectlevel4id006003003002"></a>
<h4>Bedeutung </h4>
<p>
Der Kopf der <a name="ixa100410"><a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a></a>-Schleife
besteht aus drei Ausdrücken, die jeder für sich optional
sind:
<ul>
<li>Der init-Ausdruck wird einmal vor dem Start der Schleife aufgerufen.
Er dient dazu, Initialisierungen durchzuführen, die durch die
Auswertung von Ausdrücken mit Nebeneffekten verursacht werden.
Der Rückgabewert der Ausdrücke wird vollständig ignoriert.
<li>Der init-Teil darf auch aus mehreren Ausdrücken bestehen,
wenn die einzelnen Teilausdrücke durch Kommata getrennt sind.
Diese syntaktische Erweiterung ist allerdings nur innerhalb des Initialisierungsteils
einer <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
erlaubt. Einen allgemeinen Komma-Operator (wie in C und C++) gibt
es in Java nicht.
<li>Fehlt der Initialisierungsteil, wird keine Initialisierung im
Kopf der Schleife durchgeführt.
<li>Der init-Teil darf auch Variablendeklarationen enthalten, beispielsweise,
um einen Schleifenzähler zu erzeugen. Die Variablen müssen
bei der Deklaration initialisiert werden. Sichtbarkeit und Lebensdauer
erstrecken sich auf den Block, der die Schleifenanweisungen enthält.
Damit ist es möglich, den Namen einer Schleifenvariablen innerhalb
einer Methode mehrfach zu deklarieren.
<li>Der test-Teil bildet den Testausdruck der Schleife. Analog zur
while-Schleife wird er am Anfang der Schleife ausgeführt, und
die Schleifenanweisung wird nur ausgeführt, wenn die Auswertung
des Testausdrucks true ergibt. Fehlt der Testausdruck, so setzt der
Compiler an seiner Stelle die Konstante <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
ein.
<li>Der update-Ausdruck dient dazu, den Schleifenzähler zu verändern.
Er wird nach jedem Durchlauf der Schleife ausgewertet, bevor der Testausdruck
das nächste Mal ausgewertet wird. Wie der init-Teil darf auch
der update-Teil aus mehreren Ausdrücken bestehen. Der Rückgabewert
des Ausdrucks wird ignoriert. Fehlt der update-Ausdruck, so wird keine
automatische Modifikation des Schleifenzählers durchgeführt.
</ul>
<!-- Section -->
<a name="sectlevel4id006003003003"></a>
<h4>break und continue </h4>
<p>
In Java gibt es zwei weitere Möglichkeiten, die normale Auswertungsreihenfolge
in einer Schleife zu verändern. Taucht innerhalb einer Schleife
eine <a name="ixa100411"><a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a></a>-Anweisung
auf, wird die Schleife verlassen und das Programm mit der ersten Anweisung
nach der Schleife fortgesetzt. Taucht dagegen eine <a name="ixa100412"><a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a></a>-Anweisung
auf, springt das Programm an das Ende des Schleifenrumpfs und beginnt
mit der nächsten Iteration.
<p>
In der einfachsten Form arbeiten <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>
und <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>
genauso wie in C und C++. Beide Anweisungen können innerhalb
von <a name="ixa100413"><a href="index_d.html#ixb100077"><font color=#000080><tt>do</tt></font></a></a>-,
<a name="ixa100414"><a href="index_w.html#ixb100076"><font color=#000080><tt>while</tt></font></a></a>-
und <a name="ixa100415"><a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a></a>-Schleifen
verwendet werden. Befindet sich ein <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>
innerhalb einer mehrfach geschachtelten Schleife, so verläßt
es die innerste Schleife<a name="ixa100416"></a>. Dies gilt analog
für <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>.
Neben dieser einfachen Form von <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>
und <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>
gibt es in Java noch die mit einem Label versehene Form:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
break Label;
continue Label;
</pre>
</font>
</td>
</tr>
</table>
<p>
Zu jedem mit einem Label versehenen <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>
oder <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>
muss es eine mit einem Label versehene Kontrollstruktur geben, die
diese Anweisung umschließt. Oft wird ein mit einem Label versehenes
<a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>
verwendet, um zwei oder mehr ineinander geschachtelte Schleifen zu
beenden:
<a name="labelbreak"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#00AA00">/* Listing0606.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.io.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing0606
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">008 </font> {
<font color="#555555">009 </font> <font color="#006699">int</font>[][] data = <font color="#0000AA">new</font> <font color="#006699">int</font>[10][10];
<font color="#555555">010 </font>
<font color="#555555">011 </font> <font color="#00AA00">//Multiplikationstafel erstellen</font>
<font color="#555555">012 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i <= 10; ++i) {
<font color="#555555">013 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> j = 1; j <= 10; ++j) {
<font color="#555555">014 </font> data[i - 1][j - 1] = i * j;
<font color="#555555">015 </font> }
<font color="#555555">016 </font> }
<font color="#555555">017 </font>
<font color="#555555">018 </font> <font color="#00AA00">//Produkt größer 43 suchen <a name="labelbreak.a"></a></font>
<font color="#555555">019 </font> loop1:
<font color="#555555">020 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i <= 10; ++i) {
<font color="#555555">021 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> j = 1; j <= 10; ++j) {
<font color="#555555">022 </font> <font color="#0000AA">if</font> (data[i - 1][j - 1] > 43) {
<font color="#555555">023 </font> System.out.println(i + <font color="#0000FF">"*"</font> + j + <font color="#0000FF">"="</font> + (i*j));
<font color="#555555">024 </font> <font color="#0000AA">break</font> loop1;
<font color="#555555">025 </font> }
<font color="#555555">026 </font> } <a name="labelbreak.c"></a>
<font color="#555555">027 </font> } <a name="labelbreak.b"></a>
<font color="#555555">028 </font> }
<font color="#555555">029 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing0606.java"><font color="#000055" size=-1>Listing0606.java</font></a></td>
</tr>
</table>
<i>
Listing 6.6: Das gelabelte break</i></p>
<p>
Das Programm erstellt zunächst eine Multiplikationstafel für
die Zahlen von 1 bis 10. Ab <a href="k100043.html#labelbreak.a">Zeile 018</a>
sucht es dann in einer geschachtelten Schleife nach den ersten beiden
Faktoren, deren Produkt 43 übersteigt. Die äußere
der beiden Schleifen hat das Label <font color="#000077"><tt>loop1</tt></font>,
die innere ist ohne Label. Wenn die Anweisung <font color="#000077"><tt>break
loop1;</tt></font> ausgeführt wird, springt das Programm an das
Ende des mit dem Label <font color="#000077"><tt>loop1</tt></font>
markierten Anweisungsblocks, also hinter die geschweifte Klammer in
<a href="k100043.html#labelbreak.b">Zeile 027</a>. Hätten wir
ein ungelabeltes <font color="#000077"><tt>break</tt></font> verwendet,
wäre das Programm lediglich bis an das Ende von <a href="k100043.html#labelbreak.c">Zeile 026</a>
gesprungen, und die äußere Schleife hätte eine weitere
Iteration durchgeführt.
<p>
Vollkommen analog zu der mit einem Label versehenen <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>-Anweisung
kann auch die mit einem Label versehene Form der <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>-Anweisung
verwendet werden, um die nächste Iteration einer weiter außen
liegenden Schleife einzuleiten.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Die <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>-
und <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>-Anweisungen,
insbesondere die mit einem Label versehenen, stellen <a name="ixa100417"><i>Sprunganweisungen</i></a>
dar. Im Vergleich zu den <font color="#000077"><tt>goto</tt></font>-Anweisungen
anderer Programmiersprachen sind ihre Fähigkeiten aber auf kontrollierte
Sprünge in Schleifenanweisungen beschränkt. Eine <i>allgemeine</i>
Sprunganweisung gibt es in Java nicht (die Sprachdesigner haben sich
aber scheinbar ein Hintertürchen offengelassen und <font color="#000077"><tt>goto</tt></font>
zu einem reservierten Wort erklärt).</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Die Bedeutung von gelabelten <a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>-
und <a href="index_c.html#ixb100081"><font color=#000080><tt>continue</tt></font></a>-Anweisungen
ist in der Praxis nicht so groß, wie man vermuten könnte.
Unbedacht eingesetzt, können sie die Lesbarkeit eines Programms
sogar vermindern. Eine weitere nützliche Anwendung des gelabelten
<a href="index_b.html#ixb100080"><font color=#000080><tt>break</tt></font></a>
besteht darin, eine Schleife aus einer darin liegenden <a href="index_s.html#ixb100079"><font color=#000080><tt>switch</tt></font></a>-Anweisung
zu verlassen.
<!-- Section -->
<a name="sectlevel4id006003003004"></a>
<h4>Die erweiterte for-Schleife </h4>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Mit Java 5 wurde die <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
um eine syntaktische Variante erweitert, die den Umgang mit listenartigen
Datenstrukturen, wie Collections und Arrays, vereinfacht. Diese, auch
als »foreach« <a name="ixa100418"></a><a name="ixa100419"></a>
bezeichnete Variante wird erst nach der Behandlung von Objekten (<a href="k100046.html#kapiteloop1">Kapitel 7</a>
bis <a href="k100064.html#kapiteloop4">Kapitel 10</a>) und der Einführung
von Collections (<a href="k100090.html#kapitelcollections1">Kapitel 14</a>
und <a href="k100097.html#kapitelcollections2">Kapitel 15</a>) voll
verständlich. Aus Gründen der Systematik soll sie trotzdem
an dieser Stelle erläutert werden; der Abschnitt kann aber beim
ersten Lesen übersprungen werden.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#FF9900">
<tr>
<td><font color="#FFFFFF"> JDK1.1-6.0 </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#FF9900"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Die erweiterte <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
hat folgende Syntax:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
for (formalerparameter : ausdruck)
anweisung;
</pre>
</font>
</td>
</tr>
</table>
<p>
»formalerparameter« ist dabei eine aus Datentyp und Variablenname
bestehende Parameterdeklaration der Art <font color="#000077"><tt>Integer
i</tt></font> oder <font color="#000077"><tt>Object o</tt></font>,
und »ausdruck« ist eine Instanz oder ein Ausdruck des Typs
<a name="ixa100420"><a href="index_j.html#ixb100357"><font color=#000080><tt>java.lang.Iterable</tt></font></a></a><a name="ixa100421"></a>
oder ein Array. Angewendet auf eine Collection hat die erweiterte
<a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
dieselbe Bedeutung wie folgendes Codestück:
<font color="#000077">
<pre>
for (Iterator it = ausdruck.iterator(); it.hasNext(); ) {
formalerparameter = it.next();
anweisung
}
</pre>
</font>
<p>
Ein einfaches Beispiel zeigt die Anwendung in der Praxis:
<a name="listingid006007"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> printVector1(Vector v)
<font color="#555555">002 </font>{
<font color="#555555">003 </font> <font color="#0000AA">for</font> (Iterator it = v.iterator(); it.hasNext(); ) {
<font color="#555555">004 </font> Object o = it.next();
<font color="#555555">005 </font> System.out.println(o);
<font color="#555555">006 </font> }
<font color="#555555">007 </font>}
<font color="#555555">008 </font>
<font color="#555555">009 </font><font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> printVector2(Vector v)
<font color="#555555">010 </font>{
<font color="#555555">011 </font> <font color="#0000AA">for</font> (Object o : v) {
<font color="#555555">012 </font> System.out.println(o);
<font color="#555555">013 </font> }
<font color="#555555">014 </font>}</pre>
</font>
</td>
</tr>
</table>
<i>
Listing 6.7: Anwendung der erweiterten for-Schleife auf Collections</i></p>
<p>
In <font color="#000077"><tt>printVector1</tt></font> wird eine konventionelle
<a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
verwendet, um die Elemente des Vectors auf der Konsole auszugeben.
Dazu wird zunächst ein <a href="index_i.html#ixb100125"><font color=#000080><tt>Iterator</tt></font></a>
deklariert, am Anfang der Schleife initialisiert und bei jedem Schleifendurchlauf
darauf zugegriffen. Das ist etwas umständlich und syntaktisch
nicht sehr elegant. Die Methode <font color="#000077"><tt>printVector2</tt></font>
verwendet dagegen die erweiterte <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
und kann völlig auf die explizite Deklaration und Verwendung
des Iterators verzichten. Sie hat die gleiche Bedeutung wie die erste
Variante. Der Code ist nun einfacher zu lesen und intuitiv zu verstehen
(der Doppelpunkt in der Schleifendeklaration wird wie »in«
gelesen).
<p>
Die erweiterte <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
läßt sich auch auf Arrays anwenden. Sie ist dann eine Kurzform
für folgendes Codestück:
<font color="#000077">
<pre>
Typ[] a = ausdruck;
for (int i = 0; i < a.length; i++) {
formalerparameter = a[i];
statement
}
</pre>
</font>
<p>
Daß dies ähnlich nützlich ist wie die Anwendung auf
Collections, zeigt folgendes Beispiel:
<a name="listingid006008"></a>
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#DDDDDD">
<tr>
<td valign=top>
<font color="#000055">
<pre>
<font color="#555555">001 </font><font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> printArray1(<font color="#006699">int</font>[] args)
<font color="#555555">002 </font>{
<font color="#555555">003 </font> <font color="#00AA00">// Ausgabe aller Elemente des Arrays über den Index</font>
<font color="#555555">004 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < args.length; ++i) {
<font color="#555555">005 </font> System.out.println(args[i]);
<font color="#555555">006 </font> }
<font color="#555555">007 </font>}
<font color="#555555">008 </font>
<font color="#555555">009 </font><font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> printArray2(<font color="#006699">int</font>[] args)
<font color="#555555">010 </font>{
<font color="#555555">011 </font> <font color="#00AA00">// Ausgabe aller Elemente des Arrays über die erweiterte</font>
<font color="#555555">012 </font> <font color="#00AA00">// for-Schleife (gelesen: für all a im Array args)</font>
<font color="#555555">013 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> a : args) {
<font color="#555555">014 </font> System.out.println(a);
<font color="#555555">015 </font> }
<font color="#555555">016 </font>}</pre>
</font>
</td>
</tr>
</table>
<i>
Listing 6.8: Anwendung der erweiterten for-Schleife auf Arrays</i></p>
<p>
So wird der Code einfacher und klarer.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Die erweiterte <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
wurde nicht entwickelt, um <i>alle</i> Spezialfälle eines Collection-
oder Array-Durchlaufs abzubilden. Sie will lediglich den vermutlich
häufigsten Fall vereinfachen, nämlich das einmalige Durchlaufen
aller Elemente von vorne nach hinten. Für Sonderfälle ist
nach wie vor die konventionelle <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife
zuständig. Zu diesen zählen beispielsweise:
<ul>
<li>Das Durchlaufen der Collection von hinten nach vorn
<li>Das simultane Durchlaufen mehrerer Collections
<li>Das Löschen oder Ändern einzelner Elemente während
des Durchlaufs
</ul>
</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#000077">
<tr>
<td><font color="#FFFFFF"> Hinweis </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#000077"><img src="trp1_1.gif"></td>
</tr>
</table>
<hr>
<table border=0 cellpadding=0 cellspacing=1 width="100%">
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="cover.html"> Titel </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100003.html"> Inhalt </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="search.html"> Suchen </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="index.html"> Index </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/index.html" onClick="this.href=getDocIndex()"> DOC </a>
<td align="right">Handbuch der Java-Programmierung, 5. Auflage, Addison
Wesley, Version 5.0.1
<tr bgcolor="#EEFFCC">
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100040.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100042.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100044.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100046.html"> >> </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="../jdkdocs/api/index.html" onClick="this.href=getApiIndex()"> API </a>
<td align="right">© 1998, 2007 Guido Krüger & Thomas
Stark, <a href="http://www.javabuch.de">http://www.javabuch.de</a>
</table>
<a name="endofbody"></a>
</body>
</html>
|