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