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
|
<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,k100098.html;107,k100100.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="k100098.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100100.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="sectionlists"></a>
<h2>15.2 Die Collection des Typs List </h2>
<hr>
<ul>
<li><a href="k100099.html#sectionlists">15.2 Die Collection des Typs List</a>
<ul>
<li><a href="k100099.html#sectlevel3id015002001">15.2.1 Abstrakte Eigenschaften</a>
<li><a href="k100099.html#sectlevel3id015002002">15.2.2 Implementierungen</a>
</ul>
</ul>
<hr>
<!-- Section -->
<a name="sectlevel3id015002001"></a>
<h3>15.2.1 Abstrakte Eigenschaften </h3>
<p>
Eine Collection vom Typ <a name="ixa100937"><a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a></a>
ist eine geordnete Menge von Objekten, auf die entweder sequenziell
oder über ihren Index (ihre Position in der Liste) zugegriffen
werden kann. Wie bei Arrays hat das erste Element den Index 0 und
das letzte den Index <font color="#000077"><tt>size() - 1</tt></font>.
Es ist möglich, an einer beliebigen Stelle der Liste ein Element
einzufügen oder zu löschen. Die weiter hinten stehenden
Elemente werden dann entsprechend nach rechts bzw. links verschoben.
Des weiteren gibt es Methoden, um Elemente in der Liste zu suchen.
<p>
Das Interface <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>
ist direkt aus <a href="index_c.html#ixb100721"><font color=#000080><tt>Collection</tt></font></a>
abgeleitet und erbt somit dessen Methoden. Zusätzlich gibt es
einige neue Methoden, die zum wahlfreien Zugriff auf die Elemente
benötigt werden. Um Elemente in die Liste einzufügen, können
die Methoden <a name="ixa100938"><a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a></a>
und <a name="ixa100939"><a href="index_a.html#ixb100728"><font color=#000080><tt>addAll</tt></font></a></a>
verwendet werden:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
void add(int index, Object element)
boolean add(Object o)
boolean addAll(Collection c)
boolean addAll(int index, Collection c)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/util/List.html" onClick="this.href=getApiDoc('java.util.List')"><font color="#660066" size=-1>java.util.List</font></a></td>
</tr>
</table>
<p>
Mit <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
wird ein einfaches Element in die Liste eingefügt. Wenn die Methode
mit einem einzelnen <a href="index_o.html#ixb100224"><font color=#000080><tt>Object</tt></font></a>
als Parameter aufgerufen wird, hängt sie das Element an das Ende
der Liste an. Wird zusätzlich der Index angegeben, so wird das
Element an der spezifizierten Position eingefügt und alle übrigen
Elemente um eine Position nach rechts geschoben. Mit <a href="index_a.html#ixb100728"><font color=#000080><tt>addAll</tt></font></a>
kann eine komplette Collection in die Liste eingefügt werden.
Auch hier können die Elemente wahlweise an das Ende angehängt
oder an einer beliebigen Stelle in der Liste eingefügt werden.
<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>
Der Rückgabewert von <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
ist <a href="index_t.html#ixb100233"><font color=#000080><tt>true</tt></font></a>,
wenn die Liste durch den Aufruf von <a href="index_a.html#ixb100727"><font color=#000080><tt>add</tt></font></a>
verändert, also das Element hinzugefügt wurde. Er ist <a href="index_f.html#ixb100234"><font color=#000080><tt>false</tt></font></a>,
wenn die Liste nicht verändert wurde. Das kann beispielsweise
dann der Fall sein, wenn die Liste keine Doubletten erlaubt und ein
bereits vorhandenes Element noch einmal eingefügt werden soll.
Konnte das Element dagegen aus einem anderen Grund nicht eingefügt
werden, wird eine Ausnahme des Typs <a href="index_u.html#ixb100726"><font color=#000080><tt>UnsupportedOperationException</tt></font></a>,
<a href="index_c.html#ixb100729"><font color=#000080><tt>ClassCastException</tt></font></a>
oder <a name="ixa100940"><a href="index_i.html#ixb100366"><font color=#000080><tt>IllegalArgumentException</tt></font></a></a>
ausgelöst.</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>
Das Löschen von Elementen kann mit den Methoden <a name="ixa100941"><a href="index_r.html#ixb100730"><font color=#000080><tt>remove</tt></font></a></a>,
<a name="ixa100942"><a href="index_r.html#ixb100731"><font color=#000080><tt>removeAll</tt></font></a></a>
und <a name="ixa100943"><a href="index_r.html#ixb100732"><font color=#000080><tt>retainAll</tt></font></a></a>
erfolgen:
<p>
<table border=0 cellspacing=0 cellpadding=0 width=100% bgcolor="#EEFFCC">
<tr>
<td valign=top width=100%>
<font color="#660066">
<pre>
Object remove(int index)
boolean remove(Object o)
boolean removeAll(Collection c)
boolean retainAll(Collection c)
</pre>
</font>
</td>
<td valign=top>
<a href="../jdkdocs/api/java/util/List.html" onClick="this.href=getApiDoc('java.util.List')"><font color="#660066" size=-1>java.util.List</font></a></td>
</tr>
</table>
<p>
An <a href="index_r.html#ixb100730"><font color=#000080><tt>remove</tt></font></a>
kann dabei wahlweise der Index des zu löschenden Objekts oder
das Objekt selbst übergeben werden. Mit <a href="index_r.html#ixb100731"><font color=#000080><tt>removeAll</tt></font></a>
werden alle Elemente gelöscht, die auch in der als Argument übergebenen
Collection enthalten sind, und <a href="index_r.html#ixb100732"><font color=#000080><tt>retainAll</tt></font></a>
löscht alle Elemente außer den in der Argument-Collection
enthaltenen.
<!-- Section -->
<a name="sectlevel3id015002002"></a>
<h3>15.2.2 Implementierungen </h3>
<p>
Das Interface <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>
wird seit dem JDK 1.2 von verschiedenen Klassen implementiert:
<ul>
<li>Die Klasse <a name="ixa100944"><a href="index_a.html#ixb100720"><font color=#000080><tt>AbstractList</tt></font></a></a>
ist eine abstrakte Basisklasse, bei der alle optionalen Methoden die
Ausnahme <a href="index_u.html#ixb100726"><font color=#000080><tt>UnsupportedOperationException</tt></font></a>
auslösen und diverse obligatorische Methoden als <a href="index_a.html#ixb100440"><font color=#000080><tt>abstract</tt></font></a>
deklariert wurden. Sie dient als Basisklasse für eigene <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>-Implementierungen.
<li>Die Klasse <a name="ixa100945"><a href="index_l.html#ixb100695"><font color=#000080><tt>LinkedList</tt></font></a></a>
realisiert eine Liste, deren Elemente als doppelt verkettete lineare
Liste gehalten werden. Ihre Einfüge- und Löschoperationen
sind im Prinzip (viele Elemente vorausgesetzt) performanter als die
der <a href="index_a.html#ixb100719"><font color=#000080><tt>ArrayList</tt></font></a>.
Der wahlfreie Zugriff ist dagegen normalerweise langsamer.
<li>Die Klasse <a name="ixa100946"><a href="index_a.html#ixb100719"><font color=#000080><tt>ArrayList</tt></font></a></a>
implementiert die Liste als Array von Elementen, das bei Bedarf vergrößert
wird. Hier ist der wahlfreie Zugriff schneller, aber bei großen
Elementzahlen kann das Einfügen und Löschen länger
dauern als bei einer <a href="index_l.html#ixb100695"><font color=#000080><tt>LinkedList</tt></font></a>.
<li>Aus Gründen der Vereinheitlichung implementiert seit dem
JDK 1.2 auch die Klasse <a name="ixa100947"><a href="index_v.html#ixb100120"><font color=#000080><tt>Vector</tt></font></a></a>
das <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>-Interface.
Neben den bereits in <a href="k100092.html#abschnittvector">Abschnitt 14.2</a>
erwähnten Methoden besitzt ein 1.2-<a href="index_v.html#ixb100120"><font color=#000080><tt>Vector</tt></font></a>
also auch die entsprechenden Methoden des <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>-Interfaces.
</ul>
<p>
Soll im eigenen Programm eine Liste verwendet werden, stellt sich
die Frage, welche der genannten Implementierungen dafür am besten
geeignet ist. Während die Klasse <a href="index_a.html#ixb100720"><font color=#000080><tt>AbstractList</tt></font></a>
nur als Basisklasse eigener Listenklassen sinnvoll verwendet werden
kann, ist die Entscheidung für eine der drei übrigen Klassen
von den Spezifika der jeweiligen Anwendung abhängig. Bleibt die
Liste klein, wird hauptsächlich wahlfrei darauf zugegriffen;
überwiegen die lesenden gegenüber den schreibenden Zugriffen
deutlich, so liefert die <a href="index_a.html#ixb100719"><font color=#000080><tt>ArrayList</tt></font></a>
die besten Ergebnisse. Ist die Liste dagegen sehr groß und werden
häufig Einfügungen und Löschungen vorgenommen, ist
wahrscheinlich die <a href="index_l.html#ixb100695"><font color=#000080><tt>LinkedList</tt></font></a>
die bessere Wahl. Wird von mehreren Threads gleichzeitig auf die Liste
zugegriffen, kann die Klasse <a href="index_v.html#ixb100120"><font color=#000080><tt>Vector</tt></font></a>
verwendet werden, denn ihre Methoden sind bereits weitgehend als <a href="index_s.html#ixb100715"><font color=#000080><tt>synchronized</tt></font></a>
deklariert. Weitere Untersuchungen zur Performance der Listentypen
sind in <a href="k100314.html#tuningvectorlist">Abschnitt 50.2.3</a>
zu finden.
<p>
Das folgende Beispiel zeigt das Anlegen und Bearbeiten zweier unterschiedlicher
Listen:
<a name="listingid015001"></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">/* Listing1501.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> Listing1501
<font color="#555555">006 </font>{
<font color="#555555">007 </font> <font color="#0000AA">static</font> <font color="#006699">void</font> fillList(List list)
<font color="#555555">008 </font> {
<font color="#555555">009 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < 10; ++i) {
<font color="#555555">010 </font> list.add(<font color="#0000FF">""</font> + i);
<font color="#555555">011 </font> }
<font color="#555555">012 </font> list.remove(3);
<font color="#555555">013 </font> list.remove(<font color="#0000FF">"5"</font>);
<font color="#555555">014 </font> }
<font color="#555555">015 </font>
<font color="#555555">016 </font> <font color="#0000AA">static</font> <font color="#006699">void</font> printList(List list)
<font color="#555555">017 </font> {
<font color="#555555">018 </font> <font color="#0000AA">for</font> (<font color="#006699">int</font> i = 0; i < list.size(); ++i) {
<font color="#555555">019 </font> System.out.println((String)list.get(i));
<font color="#555555">020 </font> }
<font color="#555555">021 </font> System.out.println(<font color="#0000FF">"---"</font>);
<font color="#555555">022 </font> }
<font color="#555555">023 </font>
<font color="#555555">024 </font> <font color="#0000AA">public</font> <font color="#0000AA">static</font> <font color="#006699">void</font> main(String[] args)
<font color="#555555">025 </font> {
<font color="#555555">026 </font> <font color="#00AA00">//Erzeugen der LinkedList</font>
<font color="#555555">027 </font> LinkedList list1 = <font color="#0000AA">new</font> LinkedList();
<font color="#555555">028 </font> fillList(list1);
<font color="#555555">029 </font> printList(list1);
<font color="#555555">030 </font> <font color="#00AA00">//Erzeugen der ArrayList</font>
<font color="#555555">031 </font> ArrayList list2 = <font color="#0000AA">new</font> ArrayList();
<font color="#555555">032 </font> fillList(list2);
<font color="#555555">033 </font> printList(list2);
<font color="#555555">034 </font> <font color="#00AA00">//Test von removeAll</font>
<font color="#555555">035 </font> list2.remove(<font color="#0000FF">"0"</font>);
<font color="#555555">036 </font> list1.removeAll(list2);
<font color="#555555">037 </font> printList(list1);
<font color="#555555">038 </font> }
<font color="#555555">039 </font>}</pre>
</font>
</td>
<td valign=top align=right>
<a href="../examples/Listing1501.java"><font color="#000055" size=-1>Listing1501.java</font></a></td>
</tr>
</table>
<i>
Listing 15.1: Anlegen und Bearbeiten zweier Listen</i></p>
<p>
Hierbei wird zunächst je eine Liste des Typs <a href="index_l.html#ixb100695"><font color=#000080><tt>LinkedList</tt></font></a>
und <a href="index_a.html#ixb100719"><font color=#000080><tt>ArrayList</tt></font></a>
angelegt; beide werden durch Aufruf von <font color="#000077"><tt>fillList</tt></font>
identisch gefüllt. Der Parameter <font color="#000077"><tt>list</tt></font>
hat den Typ <a href="index_l.html#ixb100717"><font color=#000080><tt>List</tt></font></a>
und akzeptiert damit beliebige Objekte, die dieses Interface implementieren.
Tatsächlich spielt es für <font color="#000077"><tt>fillList</tt></font>
keine Rolle, welche konkrete Listenklasse verwendet wurde, und die
nachfolgenden Ausgabemethoden geben jeweils genau dasselbe aus:
<font color="#333300">
<pre>
0
1
2
4
6
7
8
9
---
0
1
2
4
6
7
8
9
---
0
---
</pre>
</font>
<p>
Anschließend wird aus <font color="#000077"><tt>list2</tt></font>
das Element »0« entfernt und dann aus <font color="#000077"><tt>list1</tt></font>
alle Elemente gelöscht, die noch in <font color="#000077"><tt>list2</tt></font>
enthalten sind. Nach dem Aufruf von <a href="index_r.html#ixb100731"><font color=#000080><tt>removeAll</tt></font></a>
verbleibt also nur noch das (zu diesem Zeitpunkt nicht mehr in <font color="#000077"><tt>list2</tt></font>
enthaltene) Element »0« in <font color="#000077"><tt>list1</tt></font>
und wird durch den folgenden Aufruf von <font color="#000077"><tt>printList</tt></font>
ausgegeben.
<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="k100098.html"> < </a>
<td width="7%" align=center bgcolor="#DDCC99"><a href="k100100.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>
|