1 |
dl |
1.1 |
/* |
2 |
|
|
* %W% %E% |
3 |
|
|
* |
4 |
jsr166 |
1.14 |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
5 |
dl |
1.1 |
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
package java.util; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* Resizable-array implementation of the <tt>List</tt> interface. Implements |
12 |
|
|
* all optional list operations, and permits all elements, including |
13 |
|
|
* <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, |
14 |
|
|
* this class provides methods to manipulate the size of the array that is |
15 |
|
|
* used internally to store the list. (This class is roughly equivalent to |
16 |
|
|
* <tt>Vector</tt>, except that it is unsynchronized.)<p> |
17 |
|
|
* |
18 |
|
|
* The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>, |
19 |
|
|
* <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant |
20 |
|
|
* time. The <tt>add</tt> operation runs in <i>amortized constant time</i>, |
21 |
|
|
* that is, adding n elements requires O(n) time. All of the other operations |
22 |
|
|
* run in linear time (roughly speaking). The constant factor is low compared |
23 |
|
|
* to that for the <tt>LinkedList</tt> implementation.<p> |
24 |
|
|
* |
25 |
|
|
* Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is |
26 |
|
|
* the size of the array used to store the elements in the list. It is always |
27 |
|
|
* at least as large as the list size. As elements are added to an ArrayList, |
28 |
|
|
* its capacity grows automatically. The details of the growth policy are not |
29 |
|
|
* specified beyond the fact that adding an element has constant amortized |
30 |
|
|
* time cost.<p> |
31 |
|
|
* |
32 |
|
|
* An application can increase the capacity of an <tt>ArrayList</tt> instance |
33 |
|
|
* before adding a large number of elements using the <tt>ensureCapacity</tt> |
34 |
|
|
* operation. This may reduce the amount of incremental reallocation. |
35 |
|
|
* |
36 |
|
|
* <p><strong>Note that this implementation is not synchronized.</strong> |
37 |
|
|
* If multiple threads access an <tt>ArrayList</tt> instance concurrently, |
38 |
|
|
* and at least one of the threads modifies the list structurally, it |
39 |
|
|
* <i>must</i> be synchronized externally. (A structural modification is |
40 |
|
|
* any operation that adds or deletes one or more elements, or explicitly |
41 |
|
|
* resizes the backing array; merely setting the value of an element is not |
42 |
|
|
* a structural modification.) This is typically accomplished by |
43 |
|
|
* synchronizing on some object that naturally encapsulates the list. |
44 |
|
|
* |
45 |
|
|
* If no such object exists, the list should be "wrapped" using the |
46 |
|
|
* {@link Collections#synchronizedList Collections.synchronizedList} |
47 |
|
|
* method. This is best done at creation time, to prevent accidental |
48 |
|
|
* unsynchronized access to the list:<pre> |
49 |
|
|
* List list = Collections.synchronizedList(new ArrayList(...));</pre> |
50 |
|
|
* |
51 |
|
|
* <p>The iterators returned by this class's <tt>iterator</tt> and |
52 |
|
|
* <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is |
53 |
|
|
* structurally modified at any time after the iterator is created, in any way |
54 |
|
|
* except through the iterator's own <tt>remove</tt> or <tt>add</tt> methods, |
55 |
|
|
* the iterator will throw a {@link ConcurrentModificationException}. Thus, in |
56 |
|
|
* the face of concurrent modification, the iterator fails quickly and cleanly, |
57 |
|
|
* rather than risking arbitrary, non-deterministic behavior at an undetermined |
58 |
|
|
* time in the future.<p> |
59 |
|
|
* |
60 |
|
|
* Note that the fail-fast behavior of an iterator cannot be guaranteed |
61 |
|
|
* as it is, generally speaking, impossible to make any hard guarantees in the |
62 |
|
|
* presence of unsynchronized concurrent modification. Fail-fast iterators |
63 |
|
|
* throw <tt>ConcurrentModificationException</tt> on a best-effort basis. |
64 |
|
|
* Therefore, it would be wrong to write a program that depended on this |
65 |
|
|
* exception for its correctness: <i>the fail-fast behavior of iterators |
66 |
|
|
* should be used only to detect bugs.</i><p> |
67 |
|
|
* |
68 |
|
|
* This class is a member of the |
69 |
|
|
* <a href="{@docRoot}/../guide/collections/index.html"> |
70 |
|
|
* Java Collections Framework</a>. |
71 |
|
|
* |
72 |
|
|
* @author Josh Bloch |
73 |
|
|
* @author Neal Gafter |
74 |
|
|
* @version %I%, %G% |
75 |
|
|
* @see Collection |
76 |
|
|
* @see List |
77 |
|
|
* @see LinkedList |
78 |
|
|
* @see Vector |
79 |
|
|
* @since 1.2 |
80 |
|
|
*/ |
81 |
|
|
|
82 |
|
|
public class ArrayList<E> extends AbstractList<E> |
83 |
|
|
implements List<E>, RandomAccess, Cloneable, java.io.Serializable |
84 |
|
|
{ |
85 |
|
|
private static final long serialVersionUID = 8683452581122892189L; |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* The array buffer into which the elements of the ArrayList are stored. |
89 |
|
|
* The capacity of the ArrayList is the length of this array buffer. |
90 |
|
|
*/ |
91 |
|
|
private transient Object[] elementData; |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* The size of the ArrayList (the number of elements it contains). |
95 |
|
|
* |
96 |
|
|
* @serial |
97 |
|
|
*/ |
98 |
|
|
private int size; |
99 |
|
|
|
100 |
|
|
/** |
101 |
|
|
* Constructs an empty list with the specified initial capacity. |
102 |
|
|
* |
103 |
jsr166 |
1.5 |
* @param initialCapacity the initial capacity of the list |
104 |
|
|
* @throws IllegalArgumentException if the specified initial capacity |
105 |
|
|
* is negative |
106 |
dl |
1.1 |
*/ |
107 |
|
|
public ArrayList(int initialCapacity) { |
108 |
|
|
super(); |
109 |
|
|
if (initialCapacity < 0) |
110 |
|
|
throw new IllegalArgumentException("Illegal Capacity: "+ |
111 |
|
|
initialCapacity); |
112 |
|
|
this.elementData = new Object[initialCapacity]; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
/** |
116 |
|
|
* Constructs an empty list with an initial capacity of ten. |
117 |
|
|
*/ |
118 |
|
|
public ArrayList() { |
119 |
|
|
this(10); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* Constructs a list containing the elements of the specified |
124 |
|
|
* collection, in the order they are returned by the collection's |
125 |
jsr166 |
1.17 |
* iterator. |
126 |
dl |
1.1 |
* |
127 |
|
|
* @param c the collection whose elements are to be placed into this list |
128 |
|
|
* @throws NullPointerException if the specified collection is null |
129 |
|
|
*/ |
130 |
|
|
public ArrayList(Collection<? extends E> c) { |
131 |
dl |
1.2 |
elementData = c.toArray(); |
132 |
|
|
size = elementData.length; |
133 |
|
|
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
134 |
|
|
if (elementData.getClass() != Object[].class) |
135 |
|
|
elementData = Arrays.copyOf(elementData, size, Object[].class); |
136 |
|
|
} |
137 |
jsr166 |
1.4 |
|
138 |
dl |
1.1 |
/** |
139 |
|
|
* Trims the capacity of this <tt>ArrayList</tt> instance to be the |
140 |
|
|
* list's current size. An application can use this operation to minimize |
141 |
|
|
* the storage of an <tt>ArrayList</tt> instance. |
142 |
|
|
*/ |
143 |
|
|
public void trimToSize() { |
144 |
|
|
modCount++; |
145 |
|
|
int oldCapacity = elementData.length; |
146 |
|
|
if (size < oldCapacity) { |
147 |
|
|
elementData = Arrays.copyOf(elementData, size); |
148 |
|
|
} |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
/** |
152 |
|
|
* Increases the capacity of this <tt>ArrayList</tt> instance, if |
153 |
|
|
* necessary, to ensure that it can hold at least the number of elements |
154 |
|
|
* specified by the minimum capacity argument. |
155 |
|
|
* |
156 |
jsr166 |
1.5 |
* @param minCapacity the desired minimum capacity |
157 |
dl |
1.1 |
*/ |
158 |
|
|
public void ensureCapacity(int minCapacity) { |
159 |
|
|
modCount++; |
160 |
|
|
if (minCapacity > elementData.length) |
161 |
|
|
growArray(minCapacity); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
/** |
165 |
jsr166 |
1.5 |
* Increases the capacity of the array. |
166 |
|
|
* |
167 |
|
|
* @param minCapacity the desired minimum capacity |
168 |
dl |
1.1 |
*/ |
169 |
|
|
private void growArray(int minCapacity) { |
170 |
jsr166 |
1.18 |
if (minCapacity < 0) // overflow |
171 |
|
|
throw new OutOfMemoryError(); |
172 |
dl |
1.1 |
int oldCapacity = elementData.length; |
173 |
jsr166 |
1.18 |
// Double size if small; else grow by 50% |
174 |
|
|
int newCapacity = ((oldCapacity < 64) ? |
175 |
|
|
((oldCapacity + 1) * 2) : |
176 |
|
|
((oldCapacity / 2) * 3)); |
177 |
|
|
if (newCapacity < 0) // overflow |
178 |
|
|
newCapacity = Integer.MAX_VALUE; |
179 |
|
|
if (newCapacity < minCapacity) |
180 |
|
|
newCapacity = minCapacity; |
181 |
|
|
elementData = Arrays.copyOf(elementData, newCapacity); |
182 |
dl |
1.1 |
} |
183 |
|
|
|
184 |
|
|
/** |
185 |
|
|
* Returns the number of elements in this list. |
186 |
|
|
* |
187 |
|
|
* @return the number of elements in this list |
188 |
|
|
*/ |
189 |
|
|
public int size() { |
190 |
|
|
return size; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
/** |
194 |
|
|
* Returns <tt>true</tt> if this list contains no elements. |
195 |
|
|
* |
196 |
|
|
* @return <tt>true</tt> if this list contains no elements |
197 |
|
|
*/ |
198 |
|
|
public boolean isEmpty() { |
199 |
|
|
return size == 0; |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
/** |
203 |
|
|
* Returns <tt>true</tt> if this list contains the specified element. |
204 |
|
|
* More formally, returns <tt>true</tt> if and only if this list contains |
205 |
|
|
* at least one element <tt>e</tt> such that |
206 |
|
|
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
207 |
|
|
* |
208 |
|
|
* @param o element whose presence in this list is to be tested |
209 |
|
|
* @return <tt>true</tt> if this list contains the specified element |
210 |
|
|
*/ |
211 |
|
|
public boolean contains(Object o) { |
212 |
|
|
return indexOf(o) >= 0; |
213 |
|
|
} |
214 |
|
|
|
215 |
|
|
/** |
216 |
|
|
* Returns the index of the first occurrence of the specified element |
217 |
|
|
* in this list, or -1 if this list does not contain the element. |
218 |
|
|
* More formally, returns the lowest index <tt>i</tt> such that |
219 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
220 |
|
|
* or -1 if there is no such index. |
221 |
|
|
*/ |
222 |
|
|
public int indexOf(Object o) { |
223 |
|
|
if (o == null) { |
224 |
|
|
for (int i = 0; i < size; i++) |
225 |
|
|
if (elementData[i]==null) |
226 |
|
|
return i; |
227 |
|
|
} else { |
228 |
|
|
for (int i = 0; i < size; i++) |
229 |
|
|
if (o.equals(elementData[i])) |
230 |
|
|
return i; |
231 |
|
|
} |
232 |
|
|
return -1; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
/** |
236 |
|
|
* Returns the index of the last occurrence of the specified element |
237 |
|
|
* in this list, or -1 if this list does not contain the element. |
238 |
|
|
* More formally, returns the highest index <tt>i</tt> such that |
239 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
240 |
|
|
* or -1 if there is no such index. |
241 |
|
|
*/ |
242 |
|
|
public int lastIndexOf(Object o) { |
243 |
|
|
if (o == null) { |
244 |
|
|
for (int i = size-1; i >= 0; i--) |
245 |
|
|
if (elementData[i]==null) |
246 |
|
|
return i; |
247 |
|
|
} else { |
248 |
|
|
for (int i = size-1; i >= 0; i--) |
249 |
|
|
if (o.equals(elementData[i])) |
250 |
|
|
return i; |
251 |
|
|
} |
252 |
|
|
return -1; |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
/** |
256 |
|
|
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The |
257 |
|
|
* elements themselves are not copied.) |
258 |
|
|
* |
259 |
|
|
* @return a clone of this <tt>ArrayList</tt> instance |
260 |
|
|
*/ |
261 |
|
|
public Object clone() { |
262 |
|
|
try { |
263 |
|
|
ArrayList<E> v = (ArrayList<E>) super.clone(); |
264 |
|
|
v.elementData = Arrays.copyOf(elementData, size); |
265 |
|
|
v.modCount = 0; |
266 |
|
|
return v; |
267 |
|
|
} catch (CloneNotSupportedException e) { |
268 |
|
|
// this shouldn't happen, since we are Cloneable |
269 |
|
|
throw new InternalError(); |
270 |
|
|
} |
271 |
|
|
} |
272 |
|
|
|
273 |
|
|
/** |
274 |
|
|
* Returns an array containing all of the elements in this list |
275 |
|
|
* in proper sequence (from first to last element). |
276 |
|
|
* |
277 |
|
|
* <p>The returned array will be "safe" in that no references to it are |
278 |
|
|
* maintained by this list. (In other words, this method must allocate |
279 |
|
|
* a new array). The caller is thus free to modify the returned array. |
280 |
|
|
* |
281 |
|
|
* <p>This method acts as bridge between array-based and collection-based |
282 |
|
|
* APIs. |
283 |
|
|
* |
284 |
|
|
* @return an array containing all of the elements in this list in |
285 |
|
|
* proper sequence |
286 |
|
|
*/ |
287 |
|
|
public Object[] toArray() { |
288 |
|
|
return Arrays.copyOf(elementData, size); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
/** |
292 |
|
|
* Returns an array containing all of the elements in this list in proper |
293 |
|
|
* sequence (from first to last element); the runtime type of the returned |
294 |
|
|
* array is that of the specified array. If the list fits in the |
295 |
|
|
* specified array, it is returned therein. Otherwise, a new array is |
296 |
|
|
* allocated with the runtime type of the specified array and the size of |
297 |
|
|
* this list. |
298 |
|
|
* |
299 |
|
|
* <p>If the list fits in the specified array with room to spare |
300 |
|
|
* (i.e., the array has more elements than the list), the element in |
301 |
|
|
* the array immediately following the end of the collection is set to |
302 |
|
|
* <tt>null</tt>. (This is useful in determining the length of the |
303 |
|
|
* list <i>only</i> if the caller knows that the list does not contain |
304 |
|
|
* any null elements.) |
305 |
|
|
* |
306 |
|
|
* @param a the array into which the elements of the list are to |
307 |
|
|
* be stored, if it is big enough; otherwise, a new array of the |
308 |
|
|
* same runtime type is allocated for this purpose. |
309 |
|
|
* @return an array containing the elements of the list |
310 |
|
|
* @throws ArrayStoreException if the runtime type of the specified array |
311 |
|
|
* is not a supertype of the runtime type of every element in |
312 |
|
|
* this list |
313 |
|
|
* @throws NullPointerException if the specified array is null |
314 |
|
|
*/ |
315 |
|
|
public <T> T[] toArray(T[] a) { |
316 |
|
|
if (a.length < size) |
317 |
|
|
// Make a new array of a's runtime type, but my contents: |
318 |
|
|
return (T[]) Arrays.copyOf(elementData, size, a.getClass()); |
319 |
|
|
System.arraycopy(elementData, 0, a, 0, size); |
320 |
|
|
if (a.length > size) |
321 |
|
|
a[size] = null; |
322 |
|
|
return a; |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
// Positional Access Operations |
326 |
|
|
|
327 |
jsr166 |
1.4 |
/** |
328 |
dl |
1.9 |
* Returns error message string for IndexOutOfBoundsExceptions |
329 |
dl |
1.1 |
*/ |
330 |
dl |
1.9 |
private String ioobe(int index) { |
331 |
|
|
return "Index: " + index + ", Size: " + size; |
332 |
dl |
1.1 |
} |
333 |
|
|
|
334 |
|
|
/** |
335 |
|
|
* Returns the element at the specified position in this list. |
336 |
|
|
* |
337 |
|
|
* @param index index of the element to return |
338 |
|
|
* @return the element at the specified position in this list |
339 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
340 |
|
|
*/ |
341 |
|
|
public E get(int index) { |
342 |
|
|
if (index >= size) |
343 |
dl |
1.9 |
throw new IndexOutOfBoundsException(ioobe(index)); |
344 |
dl |
1.1 |
return (E)elementData[index]; |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
/** |
348 |
|
|
* Replaces the element at the specified position in this list with |
349 |
|
|
* the specified element. |
350 |
|
|
* |
351 |
|
|
* @param index index of the element to replace |
352 |
|
|
* @param element element to be stored at the specified position |
353 |
|
|
* @return the element previously at the specified position |
354 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
355 |
|
|
*/ |
356 |
|
|
public E set(int index, E element) { |
357 |
|
|
if (index >= size) |
358 |
dl |
1.9 |
throw new IndexOutOfBoundsException(ioobe(index)); |
359 |
dl |
1.1 |
|
360 |
|
|
E oldValue = (E) elementData[index]; |
361 |
|
|
elementData[index] = element; |
362 |
|
|
return oldValue; |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
/** |
366 |
|
|
* Appends the specified element to the end of this list. |
367 |
|
|
* |
368 |
|
|
* @param e element to be appended to this list |
369 |
|
|
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
370 |
|
|
*/ |
371 |
|
|
public boolean add(E e) { |
372 |
jsr166 |
1.8 |
modCount++; |
373 |
dl |
1.7 |
int s = size; |
374 |
dl |
1.1 |
if (s >= elementData.length) |
375 |
|
|
growArray(s + 1); |
376 |
|
|
elementData[s] = e; |
377 |
dl |
1.7 |
size = s + 1; |
378 |
dl |
1.1 |
return true; |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
/** |
382 |
|
|
* Inserts the specified element at the specified position in this |
383 |
|
|
* list. Shifts the element currently at that position (if any) and |
384 |
|
|
* any subsequent elements to the right (adds one to their indices). |
385 |
|
|
* |
386 |
|
|
* @param index index at which the specified element is to be inserted |
387 |
|
|
* @param element element to be inserted |
388 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
389 |
|
|
*/ |
390 |
|
|
public void add(int index, E element) { |
391 |
|
|
int s = size; |
392 |
|
|
if (index > s || index < 0) |
393 |
dl |
1.9 |
throw new IndexOutOfBoundsException(ioobe(index)); |
394 |
jsr166 |
1.8 |
modCount++; |
395 |
dl |
1.1 |
if (s >= elementData.length) |
396 |
|
|
growArray(s + 1); |
397 |
jsr166 |
1.10 |
System.arraycopy(elementData, index, |
398 |
dl |
1.7 |
elementData, index + 1, s - index); |
399 |
dl |
1.1 |
elementData[index] = element; |
400 |
dl |
1.7 |
size = s + 1; |
401 |
dl |
1.1 |
} |
402 |
|
|
|
403 |
|
|
/** |
404 |
|
|
* Removes the element at the specified position in this list. |
405 |
|
|
* Shifts any subsequent elements to the left (subtracts one from their |
406 |
|
|
* indices). |
407 |
|
|
* |
408 |
|
|
* @param index the index of the element to be removed |
409 |
|
|
* @return the element that was removed from the list |
410 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
411 |
|
|
*/ |
412 |
|
|
public E remove(int index) { |
413 |
|
|
int s = size - 1; |
414 |
jsr166 |
1.8 |
if (index > s) |
415 |
dl |
1.9 |
throw new IndexOutOfBoundsException(ioobe(index)); |
416 |
dl |
1.1 |
modCount++; |
417 |
dl |
1.7 |
E oldValue = (E)elementData[index]; |
418 |
dl |
1.1 |
int numMoved = s - index; |
419 |
|
|
if (numMoved > 0) |
420 |
jsr166 |
1.10 |
System.arraycopy(elementData, index + 1, |
421 |
dl |
1.7 |
elementData, index, numMoved); |
422 |
jsr166 |
1.8 |
elementData[s] = null; |
423 |
dl |
1.7 |
size = s; |
424 |
|
|
return oldValue; |
425 |
dl |
1.1 |
} |
426 |
|
|
|
427 |
|
|
/** |
428 |
|
|
* Removes the first occurrence of the specified element from this list, |
429 |
|
|
* if it is present. If the list does not contain the element, it is |
430 |
|
|
* unchanged. More formally, removes the element with the lowest index |
431 |
|
|
* <tt>i</tt> such that |
432 |
|
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
433 |
|
|
* (if such an element exists). Returns <tt>true</tt> if this list |
434 |
|
|
* contained the specified element (or equivalently, if this list |
435 |
|
|
* changed as a result of the call). |
436 |
|
|
* |
437 |
|
|
* @param o element to be removed from this list, if present |
438 |
|
|
* @return <tt>true</tt> if this list contained the specified element |
439 |
|
|
*/ |
440 |
|
|
public boolean remove(Object o) { |
441 |
|
|
if (o == null) { |
442 |
|
|
for (int index = 0; index < size; index++) |
443 |
|
|
if (elementData[index] == null) { |
444 |
|
|
fastRemove(index); |
445 |
|
|
return true; |
446 |
|
|
} |
447 |
|
|
} else { |
448 |
|
|
for (int index = 0; index < size; index++) |
449 |
|
|
if (o.equals(elementData[index])) { |
450 |
|
|
fastRemove(index); |
451 |
|
|
return true; |
452 |
|
|
} |
453 |
|
|
} |
454 |
|
|
return false; |
455 |
|
|
} |
456 |
|
|
|
457 |
|
|
/* |
458 |
|
|
* Private remove method that skips bounds checking and does not |
459 |
|
|
* return the value removed. |
460 |
|
|
*/ |
461 |
|
|
private void fastRemove(int index) { |
462 |
|
|
modCount++; |
463 |
|
|
int numMoved = size - index - 1; |
464 |
|
|
if (numMoved > 0) |
465 |
|
|
System.arraycopy(elementData, index+1, elementData, index, |
466 |
|
|
numMoved); |
467 |
|
|
elementData[--size] = null; // Let gc do its work |
468 |
|
|
} |
469 |
|
|
|
470 |
|
|
/** |
471 |
|
|
* Removes all of the elements from this list. The list will |
472 |
|
|
* be empty after this call returns. |
473 |
|
|
*/ |
474 |
|
|
public void clear() { |
475 |
|
|
modCount++; |
476 |
|
|
|
477 |
|
|
// Let gc do its work |
478 |
|
|
for (int i = 0; i < size; i++) |
479 |
|
|
elementData[i] = null; |
480 |
|
|
|
481 |
|
|
size = 0; |
482 |
|
|
} |
483 |
|
|
|
484 |
|
|
/** |
485 |
|
|
* Appends all of the elements in the specified collection to the end of |
486 |
|
|
* this list, in the order that they are returned by the |
487 |
|
|
* specified collection's Iterator. The behavior of this operation is |
488 |
|
|
* undefined if the specified collection is modified while the operation |
489 |
|
|
* is in progress. (This implies that the behavior of this call is |
490 |
|
|
* undefined if the specified collection is this list, and this |
491 |
|
|
* list is nonempty.) |
492 |
|
|
* |
493 |
|
|
* @param c collection containing elements to be added to this list |
494 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call |
495 |
|
|
* @throws NullPointerException if the specified collection is null |
496 |
|
|
*/ |
497 |
|
|
public boolean addAll(Collection<? extends E> c) { |
498 |
|
|
Object[] a = c.toArray(); |
499 |
|
|
int numNew = a.length; |
500 |
|
|
ensureCapacity(size + numNew); // Increments modCount |
501 |
|
|
System.arraycopy(a, 0, elementData, size, numNew); |
502 |
|
|
size += numNew; |
503 |
|
|
return numNew != 0; |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
/** |
507 |
|
|
* Inserts all of the elements in the specified collection into this |
508 |
|
|
* list, starting at the specified position. Shifts the element |
509 |
|
|
* currently at that position (if any) and any subsequent elements to |
510 |
|
|
* the right (increases their indices). The new elements will appear |
511 |
|
|
* in the list in the order that they are returned by the |
512 |
|
|
* specified collection's iterator. |
513 |
|
|
* |
514 |
|
|
* @param index index at which to insert the first element from the |
515 |
|
|
* specified collection |
516 |
|
|
* @param c collection containing elements to be added to this list |
517 |
|
|
* @return <tt>true</tt> if this list changed as a result of the call |
518 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
519 |
|
|
* @throws NullPointerException if the specified collection is null |
520 |
|
|
*/ |
521 |
|
|
public boolean addAll(int index, Collection<? extends E> c) { |
522 |
|
|
if (index > size || index < 0) |
523 |
dl |
1.9 |
throw new IndexOutOfBoundsException(ioobe(index)); |
524 |
dl |
1.1 |
|
525 |
|
|
Object[] a = c.toArray(); |
526 |
|
|
int numNew = a.length; |
527 |
|
|
ensureCapacity(size + numNew); // Increments modCount |
528 |
|
|
|
529 |
|
|
int numMoved = size - index; |
530 |
|
|
if (numMoved > 0) |
531 |
|
|
System.arraycopy(elementData, index, elementData, index + numNew, |
532 |
|
|
numMoved); |
533 |
|
|
|
534 |
|
|
System.arraycopy(a, 0, elementData, index, numNew); |
535 |
|
|
size += numNew; |
536 |
|
|
return numNew != 0; |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
/** |
540 |
|
|
* Removes from this list all of the elements whose index is between |
541 |
|
|
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. |
542 |
|
|
* Shifts any succeeding elements to the left (reduces their index). |
543 |
|
|
* This call shortens the list by <tt>(toIndex - fromIndex)</tt> elements. |
544 |
|
|
* (If <tt>toIndex==fromIndex</tt>, this operation has no effect.) |
545 |
|
|
* |
546 |
|
|
* @param fromIndex index of first element to be removed |
547 |
|
|
* @param toIndex index after last element to be removed |
548 |
|
|
* @throws IndexOutOfBoundsException if fromIndex or toIndex out of |
549 |
|
|
* range (fromIndex < 0 || fromIndex >= size() || toIndex |
550 |
|
|
* > size() || toIndex < fromIndex) |
551 |
|
|
*/ |
552 |
|
|
protected void removeRange(int fromIndex, int toIndex) { |
553 |
|
|
modCount++; |
554 |
|
|
int numMoved = size - toIndex; |
555 |
|
|
System.arraycopy(elementData, toIndex, elementData, fromIndex, |
556 |
|
|
numMoved); |
557 |
|
|
|
558 |
|
|
// Let gc do its work |
559 |
|
|
int newSize = size - (toIndex-fromIndex); |
560 |
|
|
while (size != newSize) |
561 |
|
|
elementData[--size] = null; |
562 |
|
|
} |
563 |
|
|
|
564 |
|
|
/** |
565 |
|
|
* Save the state of the <tt>ArrayList</tt> instance to a stream (that |
566 |
|
|
* is, serialize it). |
567 |
|
|
* |
568 |
|
|
* @serialData The length of the array backing the <tt>ArrayList</tt> |
569 |
|
|
* instance is emitted (int), followed by all of its elements |
570 |
|
|
* (each an <tt>Object</tt>) in the proper order. |
571 |
|
|
*/ |
572 |
|
|
private void writeObject(java.io.ObjectOutputStream s) |
573 |
|
|
throws java.io.IOException{ |
574 |
|
|
// Write out element count, and any hidden stuff |
575 |
|
|
int expectedModCount = modCount; |
576 |
|
|
s.defaultWriteObject(); |
577 |
|
|
|
578 |
|
|
// Write out array length |
579 |
|
|
s.writeInt(elementData.length); |
580 |
|
|
|
581 |
|
|
// Write out all elements in the proper order. |
582 |
|
|
for (int i=0; i<size; i++) |
583 |
|
|
s.writeObject(elementData[i]); |
584 |
|
|
|
585 |
jsr166 |
1.12 |
if (expectedModCount != modCount) { |
586 |
dl |
1.1 |
throw new ConcurrentModificationException(); |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
} |
590 |
|
|
|
591 |
|
|
/** |
592 |
|
|
* Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, |
593 |
|
|
* deserialize it). |
594 |
|
|
*/ |
595 |
|
|
private void readObject(java.io.ObjectInputStream s) |
596 |
|
|
throws java.io.IOException, ClassNotFoundException { |
597 |
|
|
// Read in size, and any hidden stuff |
598 |
|
|
s.defaultReadObject(); |
599 |
|
|
|
600 |
|
|
// Read in array length and allocate array |
601 |
|
|
int arrayLength = s.readInt(); |
602 |
|
|
Object[] a = elementData = new Object[arrayLength]; |
603 |
|
|
|
604 |
|
|
// Read in all elements in the proper order. |
605 |
|
|
for (int i=0; i<size; i++) |
606 |
|
|
a[i] = s.readObject(); |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
|
610 |
|
|
/** |
611 |
|
|
* Returns a list-iterator of the elements in this list (in proper |
612 |
|
|
* sequence), starting at the specified position in the list. |
613 |
|
|
* Obeys the general contract of <tt>List.listIterator(int)</tt>.<p> |
614 |
|
|
* |
615 |
|
|
* The list-iterator is <i>fail-fast</i>: if the list is structurally |
616 |
|
|
* modified at any time after the Iterator is created, in any way except |
617 |
|
|
* through the list-iterator's own <tt>remove</tt> or <tt>add</tt> |
618 |
|
|
* methods, the list-iterator will throw a |
619 |
|
|
* <tt>ConcurrentModificationException</tt>. Thus, in the face of |
620 |
|
|
* concurrent modification, the iterator fails quickly and cleanly, rather |
621 |
|
|
* than risking arbitrary, non-deterministic behavior at an undetermined |
622 |
|
|
* time in the future. |
623 |
|
|
* |
624 |
|
|
* @param index index of the first element to be returned from the |
625 |
|
|
* list-iterator (by a call to <tt>next</tt>) |
626 |
|
|
* @return a ListIterator of the elements in this list (in proper |
627 |
|
|
* sequence), starting at the specified position in the list |
628 |
|
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
629 |
|
|
* @see List#listIterator(int) |
630 |
|
|
*/ |
631 |
|
|
public ListIterator<E> listIterator(int index) { |
632 |
|
|
if (index < 0 || index > size) |
633 |
dl |
1.9 |
throw new IndexOutOfBoundsException(ioobe(index)); |
634 |
dl |
1.1 |
return new ArrayListIterator(index); |
635 |
|
|
} |
636 |
jsr166 |
1.4 |
|
637 |
dl |
1.1 |
/** |
638 |
dl |
1.9 |
* {@inheritDoc} |
639 |
|
|
*/ |
640 |
|
|
public ListIterator<E> listIterator() { |
641 |
|
|
return new ArrayListIterator(0); |
642 |
|
|
} |
643 |
|
|
|
644 |
|
|
/** |
645 |
dl |
1.1 |
* Returns an iterator over the elements in this list in proper sequence. |
646 |
|
|
* |
647 |
|
|
* @return an iterator over the elements in this list in proper sequence |
648 |
|
|
*/ |
649 |
|
|
public Iterator<E> iterator() { |
650 |
|
|
return new ArrayListIterator(0); |
651 |
|
|
} |
652 |
|
|
|
653 |
|
|
/** |
654 |
dl |
1.9 |
* A streamlined version of AbstractList.ListItr |
655 |
dl |
1.1 |
*/ |
656 |
|
|
final class ArrayListIterator implements ListIterator<E> { |
657 |
|
|
int cursor; // index of next element to return; |
658 |
|
|
int lastRet; // index of last element, or -1 if no such |
659 |
|
|
int expectedModCount; // to check for CME |
660 |
|
|
|
661 |
|
|
ArrayListIterator(int index) { |
662 |
|
|
cursor = index; |
663 |
|
|
lastRet = -1; |
664 |
|
|
expectedModCount = modCount; |
665 |
|
|
} |
666 |
|
|
|
667 |
|
|
public boolean hasNext() { |
668 |
dl |
1.13 |
return cursor != size; |
669 |
dl |
1.1 |
} |
670 |
|
|
|
671 |
|
|
public boolean hasPrevious() { |
672 |
dl |
1.13 |
return cursor != 0; |
673 |
dl |
1.1 |
} |
674 |
|
|
|
675 |
|
|
public int nextIndex() { |
676 |
|
|
return cursor; |
677 |
|
|
} |
678 |
|
|
|
679 |
|
|
public int previousIndex() { |
680 |
|
|
return cursor - 1; |
681 |
|
|
} |
682 |
|
|
|
683 |
|
|
public E next() { |
684 |
dl |
1.9 |
try { |
685 |
dl |
1.1 |
int i = cursor; |
686 |
dl |
1.9 |
E next = get(i); |
687 |
|
|
lastRet = i; |
688 |
|
|
cursor = i + 1; |
689 |
|
|
return next; |
690 |
|
|
} catch (IndexOutOfBoundsException ex) { |
691 |
|
|
throw new NoSuchElementException(); |
692 |
|
|
} finally { |
693 |
|
|
if (expectedModCount != modCount) |
694 |
|
|
throw new ConcurrentModificationException(); |
695 |
dl |
1.1 |
} |
696 |
|
|
} |
697 |
jsr166 |
1.11 |
|
698 |
dl |
1.1 |
public E previous() { |
699 |
dl |
1.9 |
try { |
700 |
dl |
1.1 |
int i = cursor - 1; |
701 |
jsr166 |
1.11 |
E prev = get(i); |
702 |
dl |
1.9 |
lastRet = i; |
703 |
|
|
cursor = i; |
704 |
jsr166 |
1.11 |
return prev; |
705 |
dl |
1.9 |
} catch (IndexOutOfBoundsException ex) { |
706 |
|
|
throw new NoSuchElementException(); |
707 |
|
|
} finally { |
708 |
jsr166 |
1.12 |
if (expectedModCount != modCount) |
709 |
dl |
1.9 |
throw new ConcurrentModificationException(); |
710 |
dl |
1.1 |
} |
711 |
|
|
} |
712 |
|
|
|
713 |
|
|
public void remove() { |
714 |
|
|
if (lastRet < 0) |
715 |
|
|
throw new IllegalStateException(); |
716 |
jsr166 |
1.12 |
if (expectedModCount != modCount) |
717 |
dl |
1.1 |
throw new ConcurrentModificationException(); |
718 |
|
|
ArrayList.this.remove(lastRet); |
719 |
|
|
if (lastRet < cursor) |
720 |
|
|
cursor--; |
721 |
|
|
lastRet = -1; |
722 |
|
|
expectedModCount = modCount; |
723 |
|
|
} |
724 |
|
|
|
725 |
|
|
public void set(E e) { |
726 |
|
|
if (lastRet < 0) |
727 |
|
|
throw new IllegalStateException(); |
728 |
jsr166 |
1.12 |
if (expectedModCount != modCount) |
729 |
dl |
1.1 |
throw new ConcurrentModificationException(); |
730 |
|
|
ArrayList.this.set(lastRet, e); |
731 |
|
|
expectedModCount = modCount; |
732 |
|
|
} |
733 |
|
|
|
734 |
|
|
public void add(E e) { |
735 |
jsr166 |
1.12 |
if (expectedModCount != modCount) |
736 |
dl |
1.1 |
throw new ConcurrentModificationException(); |
737 |
dl |
1.13 |
try { |
738 |
|
|
ArrayList.this.add(cursor++, e); |
739 |
|
|
lastRet = -1; |
740 |
|
|
expectedModCount = modCount; |
741 |
|
|
} catch (IndexOutOfBoundsException ex) { |
742 |
|
|
throw new ConcurrentModificationException(); |
743 |
|
|
} |
744 |
dl |
1.1 |
} |
745 |
|
|
} |
746 |
|
|
} |