30 |
|
import java.util.function.UnaryOperator; |
31 |
|
|
32 |
|
/** |
33 |
< |
* Resizable-array implementation of the <tt>List</tt> interface. Implements |
33 |
> |
* Resizable-array implementation of the {@code List} interface. Implements |
34 |
|
* all optional list operations, and permits all elements, including |
35 |
< |
* <tt>null</tt>. In addition to implementing the <tt>List</tt> interface, |
35 |
> |
* {@code null}. In addition to implementing the {@code List} interface, |
36 |
|
* this class provides methods to manipulate the size of the array that is |
37 |
|
* used internally to store the list. (This class is roughly equivalent to |
38 |
< |
* <tt>Vector</tt>, except that it is unsynchronized.) |
38 |
> |
* {@code Vector}, except that it is unsynchronized.) |
39 |
|
* |
40 |
< |
* <p>The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>, |
41 |
< |
* <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant |
42 |
< |
* time. The <tt>add</tt> operation runs in <i>amortized constant time</i>, |
40 |
> |
* <p>The {@code size}, {@code isEmpty}, {@code get}, {@code set}, |
41 |
> |
* {@code iterator}, and {@code listIterator} operations run in constant |
42 |
> |
* time. The {@code add} operation runs in <i>amortized constant time</i>, |
43 |
|
* that is, adding n elements requires O(n) time. All of the other operations |
44 |
|
* run in linear time (roughly speaking). The constant factor is low compared |
45 |
< |
* to that for the <tt>LinkedList</tt> implementation. |
45 |
> |
* to that for the {@code LinkedList} implementation. |
46 |
|
* |
47 |
< |
* <p>Each <tt>ArrayList</tt> instance has a <i>capacity</i>. The capacity is |
47 |
> |
* <p>Each {@code ArrayList} instance has a <i>capacity</i>. The capacity is |
48 |
|
* the size of the array used to store the elements in the list. It is always |
49 |
|
* at least as large as the list size. As elements are added to an ArrayList, |
50 |
|
* its capacity grows automatically. The details of the growth policy are not |
51 |
|
* specified beyond the fact that adding an element has constant amortized |
52 |
|
* time cost. |
53 |
|
* |
54 |
< |
* <p>An application can increase the capacity of an <tt>ArrayList</tt> instance |
55 |
< |
* before adding a large number of elements using the <tt>ensureCapacity</tt> |
54 |
> |
* <p>An application can increase the capacity of an {@code ArrayList} instance |
55 |
> |
* before adding a large number of elements using the {@code ensureCapacity} |
56 |
|
* operation. This may reduce the amount of incremental reallocation. |
57 |
|
* |
58 |
|
* <p><strong>Note that this implementation is not synchronized.</strong> |
59 |
< |
* If multiple threads access an <tt>ArrayList</tt> instance concurrently, |
59 |
> |
* If multiple threads access an {@code ArrayList} instance concurrently, |
60 |
|
* and at least one of the threads modifies the list structurally, it |
61 |
|
* <i>must</i> be synchronized externally. (A structural modification is |
62 |
|
* any operation that adds or deletes one or more elements, or explicitly |
102 |
|
* @see Vector |
103 |
|
* @since 1.2 |
104 |
|
*/ |
105 |
– |
|
105 |
|
public class ArrayList<E> extends AbstractList<E> |
106 |
|
implements List<E>, RandomAccess, Cloneable, java.io.Serializable |
107 |
|
{ |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* Trims the capacity of this <tt>ArrayList</tt> instance to be the |
188 |
> |
* Trims the capacity of this {@code ArrayList} instance to be the |
189 |
|
* list's current size. An application can use this operation to minimize |
190 |
< |
* the storage of an <tt>ArrayList</tt> instance. |
190 |
> |
* the storage of an {@code ArrayList} instance. |
191 |
|
*/ |
192 |
|
public void trimToSize() { |
193 |
|
modCount++; |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
< |
* Increases the capacity of this <tt>ArrayList</tt> instance, if |
202 |
> |
* Increases the capacity of this {@code ArrayList} instance, if |
203 |
|
* necessary, to ensure that it can hold at least the number of elements |
204 |
|
* specified by the minimum capacity argument. |
205 |
|
* |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* Returns <tt>true</tt> if this list contains no elements. |
281 |
> |
* Returns {@code true} if this list contains no elements. |
282 |
|
* |
283 |
< |
* @return <tt>true</tt> if this list contains no elements |
283 |
> |
* @return {@code true} if this list contains no elements |
284 |
|
*/ |
285 |
|
public boolean isEmpty() { |
286 |
|
return size == 0; |
287 |
|
} |
288 |
|
|
289 |
|
/** |
290 |
< |
* Returns <tt>true</tt> if this list contains the specified element. |
291 |
< |
* More formally, returns <tt>true</tt> if and only if this list contains |
292 |
< |
* at least one element <tt>e</tt> such that |
293 |
< |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
290 |
> |
* Returns {@code true} if this list contains the specified element. |
291 |
> |
* More formally, returns {@code true} if and only if this list contains |
292 |
> |
* at least one element {@code e} such that |
293 |
> |
* {@code Objects.equals(o, e)}. |
294 |
|
* |
295 |
|
* @param o element whose presence in this list is to be tested |
296 |
< |
* @return <tt>true</tt> if this list contains the specified element |
296 |
> |
* @return {@code true} if this list contains the specified element |
297 |
|
*/ |
298 |
|
public boolean contains(Object o) { |
299 |
|
return indexOf(o) >= 0; |
302 |
|
/** |
303 |
|
* Returns the index of the first occurrence of the specified element |
304 |
|
* in this list, or -1 if this list does not contain the element. |
305 |
< |
* More formally, returns the lowest index <tt>i</tt> such that |
306 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
305 |
> |
* More formally, returns the lowest index {@code i} such that |
306 |
> |
* {@code Objects.equals(o, get(i))}, |
307 |
|
* or -1 if there is no such index. |
308 |
|
*/ |
309 |
|
public int indexOf(Object o) { |
322 |
|
/** |
323 |
|
* Returns the index of the last occurrence of the specified element |
324 |
|
* in this list, or -1 if this list does not contain the element. |
325 |
< |
* More formally, returns the highest index <tt>i</tt> such that |
326 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
325 |
> |
* More formally, returns the highest index {@code i} such that |
326 |
> |
* {@code Objects.equals(o, get(i))}, |
327 |
|
* or -1 if there is no such index. |
328 |
|
*/ |
329 |
|
public int lastIndexOf(Object o) { |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* Returns a shallow copy of this <tt>ArrayList</tt> instance. (The |
343 |
> |
* Returns a shallow copy of this {@code ArrayList} instance. (The |
344 |
|
* elements themselves are not copied.) |
345 |
|
* |
346 |
< |
* @return a clone of this <tt>ArrayList</tt> instance |
346 |
> |
* @return a clone of this {@code ArrayList} instance |
347 |
|
*/ |
348 |
|
public Object clone() { |
349 |
|
try { |
386 |
|
* <p>If the list fits in the specified array with room to spare |
387 |
|
* (i.e., the array has more elements than the list), the element in |
388 |
|
* the array immediately following the end of the collection is set to |
389 |
< |
* <tt>null</tt>. (This is useful in determining the length of the |
389 |
> |
* {@code null}. (This is useful in determining the length of the |
390 |
|
* list <i>only</i> if the caller knows that the list does not contain |
391 |
|
* any null elements.) |
392 |
|
* |
451 |
|
* Appends the specified element to the end of this list. |
452 |
|
* |
453 |
|
* @param e element to be appended to this list |
454 |
< |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
454 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
455 |
|
*/ |
456 |
|
public boolean add(E e) { |
457 |
|
ensureCapacityInternal(size + 1); // Increments modCount!! |
506 |
|
* Removes the first occurrence of the specified element from this list, |
507 |
|
* if it is present. If the list does not contain the element, it is |
508 |
|
* unchanged. More formally, removes the element with the lowest index |
509 |
< |
* <tt>i</tt> such that |
510 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
511 |
< |
* (if such an element exists). Returns <tt>true</tt> if this list |
509 |
> |
* {@code i} such that |
510 |
> |
* {@code Objects.equals(o, get(i))} |
511 |
> |
* (if such an element exists). Returns {@code true} if this list |
512 |
|
* contained the specified element (or equivalently, if this list |
513 |
|
* changed as a result of the call). |
514 |
|
* |
515 |
|
* @param o element to be removed from this list, if present |
516 |
< |
* @return <tt>true</tt> if this list contained the specified element |
516 |
> |
* @return {@code true} if this list contained the specified element |
517 |
|
*/ |
518 |
|
public boolean remove(Object o) { |
519 |
|
if (o == null) { |
569 |
|
* list is nonempty.) |
570 |
|
* |
571 |
|
* @param c collection containing elements to be added to this list |
572 |
< |
* @return <tt>true</tt> if this list changed as a result of the call |
572 |
> |
* @return {@code true} if this list changed as a result of the call |
573 |
|
* @throws NullPointerException if the specified collection is null |
574 |
|
*/ |
575 |
|
public boolean addAll(Collection<? extends E> c) { |
592 |
|
* @param index index at which to insert the first element from the |
593 |
|
* specified collection |
594 |
|
* @param c collection containing elements to be added to this list |
595 |
< |
* @return <tt>true</tt> if this list changed as a result of the call |
595 |
> |
* @return {@code true} if this list changed as a result of the call |
596 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
597 |
|
* @throws NullPointerException if the specified collection is null |
598 |
|
*/ |
740 |
|
} |
741 |
|
|
742 |
|
/** |
743 |
< |
* Save the state of the <tt>ArrayList</tt> instance to a stream (that |
743 |
> |
* Save the state of the {@code ArrayList} instance to a stream (that |
744 |
|
* is, serialize it). |
745 |
|
* |
746 |
< |
* @serialData The length of the array backing the <tt>ArrayList</tt> |
746 |
> |
* @serialData The length of the array backing the {@code ArrayList} |
747 |
|
* instance is emitted (int), followed by all of its elements |
748 |
< |
* (each an <tt>Object</tt>) in the proper order. |
748 |
> |
* (each an {@code Object}) in the proper order. |
749 |
|
*/ |
750 |
|
private void writeObject(java.io.ObjectOutputStream s) |
751 |
|
throws java.io.IOException{ |
767 |
|
} |
768 |
|
|
769 |
|
/** |
770 |
< |
* Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, |
770 |
> |
* Reconstitute the {@code ArrayList} instance from a stream (that is, |
771 |
|
* deserialize it). |
772 |
|
*/ |
773 |
|
private void readObject(java.io.ObjectInputStream s) |