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
|
<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,k100097.html;106,k100099.html;107,k100101.html;108,k100107.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="k100097.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100099.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100101.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100107.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 15 - Collections II
</table>
<hr>
<!-- Section -->
<a name="sectlevel2id015003"></a>
<h2>15.3 Iteratoren </h2>
<hr>
<ul>
<li><a href="k100100.html#sectlevel2id015003">15.3 Iteratoren</a>
<ul>
<li><a href="k100100.html#sectlevel3id015003001">15.3.1 Das Interface Iterator</a>
<li><a href="k100100.html#sectlevel3id015003002">15.3.2 Das Interface ListIterator</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id015003001"></a>
<h3>15.3.1 Das Interface Iterator </h3>
<p>
Auf die Collections der Prä-1.2-JDKs konnte mit Hilfe des <a name="ixa100948"><a href="index_e.html#ixb100122"><font color=#000080><tt>Enumeration</tt></font></a></a>-Interfaces
und seiner beiden Methoden <a href="index_h.html#ixb100684"><font color=#000080><tt>hasMoreElements</tt></font></a>
und <a href="index_n.html#ixb100685"><font color=#000080><tt>nextElement</tt></font></a>
zugegriffen werden. Da Objekte, die dem Durchlaufen von Collections
dienen, überall in der Informatik als <i>Iteratoren</i> bezeichnet
werden, wurde die Namensgebung des Interfaces und seiner Methoden
vielfach kritisiert. Die Designer der Collections der Version 1.2
haben sich nun dem allgemeinen Sprachgebrauch angepasst und das Interface
zum Durchlaufen der Elemente einer Collection als <a name="ixa100949"><a href="index_i.html#ixb100125"><font color=#000080><tt>Iterator</tt></font></a></a>
bezeichnet und mit folgenden Methoden ausgestattet:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
boolean hasNext()
Object next()
void remove()
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/util/Iterator.html" onClick="this.href=getApiDoc('java.util.Iterator')"><font color="#660066" size=-1>java.util.Iterator</font></a></td>
</tr>
</table>
<p>
Ein solches Iterator-Objekt kann über die Methode <a href="index_i.html#ixb100733"><font color=#000080><tt>iterator</tt></font></a>
für jede Collection des Frameworks erzeugt werden. Hierfür
wurde das Interface <a href="index_j.html#ixb100357"><font color=#000080><tt>java.lang.Iterable</tt></font></a>
definiert, welches auch von jeder Collection implementiert wird.
<p>
<a name="ixa100950"><a href="index_h.html#ixb100508"><font color=#000080><tt>hasNext</tt></font></a></a>
gibt genau dann <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>
zurück, wenn der Iterator mindestens ein weiteres Element enthält.
<a name="ixa100951"><a href="index_n.html#ixb100509"><font color=#000080><tt>next</tt></font></a></a>
liefert das nächste Element bzw. löst eine Ausnahme des
Typs <a name="ixa100952"><a href="index_n.html#ixb100734"><font color=#000080><tt>NoSuchElementException</tt></font></a></a>
aus, wenn es keine weiteren Elemente gibt. Wie beim <a href="index_e.html#ixb100122"><font color=#000080><tt>Enumeration</tt></font></a>-Interface
ist der Rückgabewert als <a href="index_o.html#ixb100224"><font color=#000080><tt>Object</tt></font></a>
deklariert und muss daher auf das passende Object gecastet werden.
Als neues Feature (gegenüber einer <a href="index_e.html#ixb100122"><font color=#000080><tt>Enumeration</tt></font></a>)
bietet ein <a href="index_i.html#ixb100125"><font color=#000080><tt>Iterator</tt></font></a>
die Möglichkeit, die Collection während der Abfrage zu ändern,
indem das zuletzt geholte Element mit der Methode <a name="ixa100953"><a href="index_r.html#ixb100730"><font color=#000080><tt>remove</tt></font></a></a>
gelöscht wird. Bei allen Collections, die das Interface <a href="index_c.html#ixb100721"><font color=#000080><tt>Collection</tt></font></a>
implementieren, kann ein Iterator zum Durchlaufen aller Elemente mit
der Methode <a name="ixa100954"><a href="index_i.html#ixb100733"><font color=#000080><tt>iterator</tt></font></a></a>
beschafft werden.
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100%>
<tr>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=1></td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top width=1000>
<p>
Dies ist auch gleichzeitig die einzige erlaubte Möglichkeit,
die Collection während der Verwendung eines Iterators zu ändern.
Alle direkt ändernden Zugriffe auf die Collection machen das
weitere Verhalten des Iterators undefiniert.</td>
<td><img src="trp1_1.gif" width=2></td>
<td valign=top>
<table border=0 cellspacing=0 cellpadding=1 width=100% bgcolor="#CC0000">
<tr>
<td><font color="#FFFFFF"> Warnung </font></td>
</tr>
</table>
</td>
<td width=1 align=left valign=top bgcolor="#CC0000"><img src="trp1_1.gif"></td>
</tr>
</table>
<p>
Wir wollen uns die Benutzung eines Iterators an einem Beispiel ansehen:
<a name="listingid015002"></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">/* Listing1502.java */</font>
<font color="#555555">002 </font>
<font color="#555555">003 </font><font color="#0000AA">import</font> java.util.*;
<font color="#555555">004 </font>
<font color="#555555">005 </font><font color="#0000AA">public</font> <font color="#0000AA">class</font> Listing1502
<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="#00AA00">//Füllen der Liste</font>
<font color="#555555">010 </font> ArrayList list = <font color="#0000AA">new</font> ArrayList();
<font color="#555555">011 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 1; i <= 20; ++i) {
<font color="#555555">012 </font> list.add(<font color="#0000FF">""</font> + i);
<font color="#555555">013 </font> }
<font color="#555555">014 </font> <font color="#00AA00">//Löschen von Elementen über Iterator</font>
<font color="#555555">015 </font> Iterator it = list.iterator();
<font color="#555555">016 </font> <font color="#0000AA">while</font> (it.hasNext()) {
<font color="#555555">017 </font> String s = (String) it.next();
<font color="#555555">018 </font> <font color="#0000AA">if</font> (s.startsWith(<font color="#0000FF">"1"</font>)) {
<font color="#555555">019 </font> it.remove();
<font color="#555555">020 </font> }
<font color="#555555">021 </font> }
<font color="#555555">022 </font> <font color="#00AA00">//Ausgeben der verbleibenden Elemente</font>
<font color="#555555">023 </font> it = list.iterator();
<font color="#555555">024 </font> <font color="#0000AA">while</font> (it.hasNext()) {
<font color="#555555">025 </font> System.out.println((String) it.next());
<font color="#555555">026 </font> }
<font color="#555555">027 </font> }
<font color="#555555">028 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1502.java"><font color="#000055" size=-1>Listing1502.java</font></a></td>
</tr>
</table>
<i>
Listing 15.2: Zugriff auf eine Collection mit einem Iterator</i></p>
<p>
Das Programm erzeugt zunächst eine Liste mit den Elementen »1«,
»2«, ..., »20«. Anschließend wird durch
Aufruf von <a href="index_i.html#ixb100733"><font color=#000080><tt>iterator</tt></font></a>
ein Iterator über alle Elemente der Liste beschafft. Mit seiner
Hilfe werden alle Elemente der Liste durchlaufen und diejenigen, die
mit »1« anfangen, gelöscht. Der zweite Durchlauf durch
die Liste zeigt dann nur noch die übriggebliebenen Elemente an:
<font color="#333300">
<pre>
2
3
4
5
6
7
8
9
20
</pre>
</font>
<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>
Seit der J2SE 5.0 gibt es eine Erweiterung der <a href="index_f.html#ixb100078"><font color=#000080><tt>for</tt></font></a>-Schleife,
mit der Collection-Klassen einfach durchlaufen werden können.
Diese, auch als foreach-Schleife bezeichnete Variante wird in <a href="k100043.html#dieforschleife">Abschnitt 6.3.3</a>
ausführlich erläutert.</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>
<!-- Section -->
<a name="sectlevel3id015003002"></a>
<h3>15.3.2 Das Interface ListIterator </h3>
<p>
Neben dem <a href="index_i.html#ixb100125"><font color=#000080><tt>Iterator</tt></font></a>-Interface
gibt es das daraus abgeleitete Interface <a name="ixa100955"><a href="index_l.html#ixb100735"><font color=#000080><tt>ListIterator</tt></font></a></a>.
Es steht nur bei Collections des Typs <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>
(und daraus abgeleiteten Klassen) zur Verfügung und bietet zusätzlich
die Möglichkeit, die Liste in beiden Richtungen zu durchlaufen,
auf den Index des nächsten oder vorigen Elements zuzugreifen,
das aktuelle Element zu verändern und ein neues Element hinzuzufügen:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
boolean hasPrevious()
Object previous()
int nextIndex()
int previousIndex()
void add(Object o)
void set(Object o)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/util/ListIterator.html" onClick="this.href=getApiDoc('java.util.ListIterator')"><font color="#660066" size=-1>java.util.ListIterator</font></a></td>
</tr>
</table>
<p>
Mit <a name="ixa100956"><a href="index_h.html#ixb100736"><font color=#000080><tt>hasPrevious</tt></font></a></a>
kann bestimmt werden, ob es <i>vor</i> der aktuellen Position ein
weiteres Element gibt; der Zugriff darauf würde mit <a name="ixa100957"><a href="index_p.html#ixb100737"><font color=#000080><tt>previous</tt></font></a></a>
erfolgen. Die Methoden <a name="ixa100958"><a href="index_n.html#ixb100738"><font color=#000080><tt>nextIndex</tt></font></a></a>
und <a name="ixa100959"><a href="index_p.html#ixb100739"><font color=#000080><tt>previousIndex</tt></font></a></a>
liefern den Index des nächsten bzw. vorigen Elements des Iterators.
Wird <a href="index_p.html#ixb100739"><font color=#000080><tt>previousIndex</tt></font></a>
am Anfang des Iterators aufgerufen, ist sein Rückgabewert -1.
Wird <a href="index_n.html#ixb100738"><font color=#000080><tt>nextIndex</tt></font></a>
am Ende aufgerufen, liefert es <font color="#000077"><tt>size()</tt></font>
als Rückgabewert. Mit <a name="ixa100960"><a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a></a>
kann ein neues Element an der Stelle in die Liste eingefügt werden,
die unmittelbar vor dem nächsten Element des Iterators liegt.
<a name="ixa100961"><a href="index_s.html#ixb100181"><font color=#000080><tt>set</tt></font></a></a>
erlaubt es, das durch den letzten Aufruf von <a href="index_n.html#ixb100509"><font color=#000080><tt>next</tt></font></a>
bzw. <a href="index_p.html#ixb100737"><font color=#000080><tt>previous</tt></font></a>
beschaffte Element zu ersetzen.
<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>
Ebenso wie beim Interface <a href="index_c.html#ixb100721"><font color=#000080><tt>Collection</tt></font></a>
sind die <i>ändernden</i> Methoden der Iteratoren optional. Falls
ein Iterator eine dieser Methoden nicht zur Verfügung stellen
will, löst er bei ihrem Aufruf eine Ausnahme des Typs <a href="index_u.html#ixb100726"><font color=#000080><tt>UnsupportedOperationException</tt></font></a>
aus. Das gilt für die Methoden <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>,
<a href="index_s.html#ixb100181"><font color=#000080><tt>set</tt></font></a>
und <a href="index_r.html#ixb100730"><font color=#000080><tt>remove</tt></font></a>.</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="k100097.html"> << </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100099.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100101.html"> > </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100107.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>
|