6 |
|
*/ |
7 |
|
|
8 |
|
package java.util; |
9 |
+ |
import java.util.*; // for javadoc (till 6280605 is fixed) |
10 |
|
|
11 |
|
/** |
12 |
|
* Linked list implementation of the <tt>List</tt> interface. Implements all |
26 |
|
* list. Operations that index into the list will traverse the list from |
27 |
|
* the beginning or the end, whichever is closer to the specified index.<p> |
28 |
|
* |
29 |
< |
* <b>Note that this implementation is not synchronized.</b> If multiple |
30 |
< |
* threads access a list concurrently, and at least one of the threads |
31 |
< |
* modifies the list structurally, it <i>must</i> be synchronized |
32 |
< |
* externally. (A structural modification is any operation that adds or |
33 |
< |
* deletes one or more elements; merely setting the value of an element is not |
34 |
< |
* a structural modification.) This is typically accomplished by |
35 |
< |
* synchronizing on some object that naturally encapsulates the list. If no |
36 |
< |
* such object exists, the list should be "wrapped" using the |
37 |
< |
* Collections.synchronizedList method. This is best done at creation time, |
38 |
< |
* to prevent accidental unsynchronized access to the list: <pre> |
39 |
< |
* List list = Collections.synchronizedList(new LinkedList(...)); |
40 |
< |
* </pre> |
29 |
> |
* <p><strong>Note that this implementation is not synchronized.</strong> |
30 |
> |
* If multiple threads access a linked list concurrently, and at least |
31 |
> |
* one of the threads modifies the list structurally, it <i>must</i> be |
32 |
> |
* synchronized externally. (A structural modification is any operation |
33 |
> |
* that adds or deletes one or more elements; merely setting the value of |
34 |
> |
* an element is not a structural modification.) This is typically |
35 |
> |
* accomplished by synchronizing on some object that naturally |
36 |
> |
* encapsulates the list. |
37 |
> |
* |
38 |
> |
* If no such object exists, the list should be "wrapped" using the |
39 |
> |
* {@link Collections#synchronizedList Collections.synchronizedList} |
40 |
> |
* method. This is best done at creation time, to prevent accidental |
41 |
> |
* unsynchronized access to the list:<pre> |
42 |
> |
* List list = Collections.synchronizedList(new LinkedList(...));</pre> |
43 |
|
* |
44 |
|
* <p>The iterators returned by this class's <tt>iterator</tt> and |
45 |
|
* <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is |
68 |
|
* @see List |
69 |
|
* @see ArrayList |
70 |
|
* @see Vector |
68 |
– |
* @see Collections#synchronizedList(List) |
71 |
|
* @since 1.2 |
72 |
|
* @param <E> the type of elements held in this collection |
73 |
|
*/ |
94 |
|
* @param c the collection whose elements are to be placed into this list |
95 |
|
* @throws NullPointerException if the specified collection is null |
96 |
|
*/ |
97 |
< |
public LinkedList(Collection<? extends E> c) { |
98 |
< |
this(); |
99 |
< |
addAll(c); |
100 |
< |
} |
97 |
> |
public LinkedList(Collection<? extends E> c) { |
98 |
> |
this(); |
99 |
> |
addAll(c); |
100 |
> |
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Returns the first element in this list. |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* Inserts the given element at the beginning of this list. |
149 |
> |
* Inserts the specified element at the beginning of this list. |
150 |
|
* |
151 |
< |
* @param e the element to be inserted at the beginning of this list |
151 |
> |
* @param e the element to add |
152 |
|
*/ |
153 |
|
public void addFirst(E e) { |
154 |
|
addBefore(e, header.next); |
155 |
|
} |
156 |
|
|
157 |
|
/** |
158 |
< |
* Appends the given element to the end of this list. (Identical in |
157 |
< |
* function to the <tt>add</tt> method; included only for consistency.) |
158 |
> |
* Appends the specified element to the end of this list. |
159 |
|
* |
160 |
< |
* @param e the element to be inserted at the end of this list |
160 |
> |
* <p>This method is equivalent to {@link #add}. |
161 |
> |
* |
162 |
> |
* @param e the element to add |
163 |
|
*/ |
164 |
|
public void addLast(E e) { |
165 |
|
addBefore(e, header); |
168 |
|
/** |
169 |
|
* Returns <tt>true</tt> if this list contains the specified element. |
170 |
|
* More formally, returns <tt>true</tt> if and only if this list contains |
171 |
< |
* at least one element <tt>e</tt> such that <tt>(o==null ? e==null |
172 |
< |
* : o.equals(e))</tt>. |
171 |
> |
* at least one element <tt>e</tt> such that |
172 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
173 |
|
* |
174 |
|
* @param o element whose presence in this list is to be tested |
175 |
|
* @return <tt>true</tt> if this list contains the specified element |
190 |
|
/** |
191 |
|
* Appends the specified element to the end of this list. |
192 |
|
* |
193 |
+ |
* <p>This method is equivalent to {@link #addLast}. |
194 |
+ |
* |
195 |
|
* @param e element to be appended to this list |
196 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
196 |
> |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
197 |
|
*/ |
198 |
|
public boolean add(E e) { |
199 |
|
addBefore(e, header); |
201 |
|
} |
202 |
|
|
203 |
|
/** |
204 |
< |
* Removes the first occurrence of the specified element in this list. If |
205 |
< |
* the list does not contain the element, it is unchanged. More formally, |
206 |
< |
* removes the element with the lowest index <tt>i</tt> such that |
207 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if such an |
208 |
< |
* element exists). |
204 |
> |
* Removes the first occurrence of the specified element from this list, |
205 |
> |
* if it is present. If this list does not contain the element, it is |
206 |
> |
* unchanged. More formally, removes the element with the lowest index |
207 |
> |
* <tt>i</tt> such that |
208 |
> |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
209 |
> |
* (if such an element exists). Returns <tt>true</tt> if this list |
210 |
> |
* contained the specified element (or equivalently, if this list |
211 |
> |
* changed as a result of the call). |
212 |
|
* |
213 |
|
* @param o element to be removed from this list, if present |
214 |
< |
* @return <tt>true</tt> if the list contained the specified element |
214 |
> |
* @return <tt>true</tt> if this list contained the specified element |
215 |
|
*/ |
216 |
|
public boolean remove(Object o) { |
217 |
|
if (o==null) { |
237 |
|
* this list, in the order that they are returned by the specified |
238 |
|
* collection's iterator. The behavior of this operation is undefined if |
239 |
|
* the specified collection is modified while the operation is in |
240 |
< |
* progress. (This implies that the behavior of this call is undefined if |
241 |
< |
* the specified Collection is this list, and this list is nonempty.) |
240 |
> |
* progress. (Note that this will occur if the specified collection is |
241 |
> |
* this list, and it's nonempty.) |
242 |
|
* |
243 |
< |
* @param c the elements to be inserted into this list |
243 |
> |
* @param c collection containing elements to be added to this list |
244 |
|
* @return <tt>true</tt> if this list changed as a result of the call |
245 |
|
* @throws NullPointerException if the specified collection is null |
246 |
|
*/ |
258 |
|
* |
259 |
|
* @param index index at which to insert the first element |
260 |
|
* from the specified collection |
261 |
< |
* @param c elements to be inserted into this list |
261 |
> |
* @param c collection containing elements to be added to this list |
262 |
|
* @return <tt>true</tt> if this list changed as a result of the call |
263 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
256 |
< |
* (<tt>index < 0 || index > size()</tt>) |
263 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
264 |
|
* @throws NullPointerException if the specified collection is null |
265 |
|
*/ |
266 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
308 |
|
/** |
309 |
|
* Returns the element at the specified position in this list. |
310 |
|
* |
311 |
< |
* @param index index of element to return |
311 |
> |
* @param index index of the element to return |
312 |
|
* @return the element at the specified position in this list |
313 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
307 |
< |
* (<tt>index < 0 || index > size()</tt>) |
313 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
314 |
|
*/ |
315 |
|
public E get(int index) { |
316 |
|
return entry(index).element; |
320 |
|
* Replaces the element at the specified position in this list with the |
321 |
|
* specified element. |
322 |
|
* |
323 |
< |
* @param index index of element to replace |
323 |
> |
* @param index index of the element to replace |
324 |
|
* @param element element to be stored at the specified position |
325 |
|
* @return the element previously at the specified position |
326 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
321 |
< |
* (<tt>index < 0 || index > size()</tt>) |
326 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
327 |
|
*/ |
328 |
|
public E set(int index, E element) { |
329 |
|
Entry<E> e = entry(index); |
339 |
|
* |
340 |
|
* @param index index at which the specified element is to be inserted |
341 |
|
* @param element element to be inserted |
342 |
< |
* |
338 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
339 |
< |
* (<tt>index < 0 || index > size()</tt>) |
342 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
343 |
|
*/ |
344 |
|
public void add(int index, E element) { |
345 |
|
addBefore(element, (index==size ? header : entry(index))); |
352 |
|
* |
353 |
|
* @param index the index of the element to be removed |
354 |
|
* @return the element previously at the specified position |
355 |
< |
* |
353 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
354 |
< |
* (<tt>index < 0 || index > size()</tt>) |
355 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
356 |
|
*/ |
357 |
|
public E remove(int index) { |
358 |
|
return remove(entry(index)); |
380 |
|
// Search Operations |
381 |
|
|
382 |
|
/** |
383 |
< |
* Returns the index in this list of the first occurrence of the |
384 |
< |
* specified element, or -1 if the List does not contain this |
385 |
< |
* element. More formally, returns the lowest index i such that |
386 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
387 |
< |
* there is no such index. |
383 |
> |
* Returns the index of the first occurrence of the specified element |
384 |
> |
* in this list, or -1 if this list does not contain the element. |
385 |
> |
* More formally, returns the lowest index <tt>i</tt> such that |
386 |
> |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
387 |
> |
* or -1 if there is no such index. |
388 |
|
* |
389 |
|
* @param o element to search for |
390 |
< |
* @return the index in this list of the first occurrence of the |
391 |
< |
* specified element, or -1 if the list does not contain this |
391 |
< |
* element |
390 |
> |
* @return the index of the first occurrence of the specified element in |
391 |
> |
* this list, or -1 if this list does not contain the element |
392 |
|
*/ |
393 |
|
public int indexOf(Object o) { |
394 |
|
int index = 0; |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* Returns the index in this list of the last occurrence of the |
413 |
< |
* specified element, or -1 if the list does not contain this |
414 |
< |
* element. More formally, returns the highest index i such that |
415 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
416 |
< |
* there is no such index. |
412 |
> |
* Returns the index of the last occurrence of the specified element |
413 |
> |
* in this list, or -1 if this list does not contain the element. |
414 |
> |
* More formally, returns the highest index <tt>i</tt> such that |
415 |
> |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, |
416 |
> |
* or -1 if there is no such index. |
417 |
|
* |
418 |
|
* @param o element to search for |
419 |
< |
* @return the index in this list of the last occurrence of the |
420 |
< |
* specified element, or -1 if the list does not contain this |
421 |
< |
* element |
419 |
> |
* @return the index of the last occurrence of the specified element in |
420 |
> |
* this list, or -1 if this list does not contain the element |
421 |
|
*/ |
422 |
|
public int lastIndexOf(Object o) { |
423 |
|
int index = size; |
486 |
|
* Adds the specified element as the tail (last element) of this list. |
487 |
|
* |
488 |
|
* @param e the element to add |
489 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
489 |
> |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
490 |
|
* @since 1.5 |
491 |
|
*/ |
492 |
|
public boolean offer(E e) { |
498 |
|
* Inserts the specified element at the front of this list. |
499 |
|
* |
500 |
|
* @param e the element to insert |
501 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
501 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
502 |
|
* @since 1.6 |
503 |
|
*/ |
504 |
|
public boolean offerFirst(E e) { |
523 |
|
* or returns <tt>null</tt> if this list is empty. |
524 |
|
* |
525 |
|
* @return the first element of this list, or <tt>null</tt> |
526 |
< |
* if this list is empty. |
526 |
> |
* if this list is empty |
527 |
|
* @since 1.6 |
528 |
|
*/ |
529 |
|
public E peekFirst() { |
537 |
|
* or returns <tt>null</tt> if this list is empty. |
538 |
|
* |
539 |
|
* @return the last element of this list, or <tt>null</tt> |
540 |
< |
* if this list is empty. |
540 |
> |
* if this list is empty |
541 |
|
* @since 1.6 |
542 |
|
*/ |
543 |
|
public E peekLast() { |
626 |
|
*/ |
627 |
|
public boolean removeLastOccurrence(Object o) { |
628 |
|
if (o==null) { |
629 |
< |
for (Entry e = header.previous; e != header; e = e.previous) { |
629 |
> |
for (Entry<E> e = header.previous; e != header; e = e.previous) { |
630 |
|
if (e.element==null) { |
631 |
|
remove(e); |
632 |
|
return true; |
633 |
|
} |
634 |
|
} |
635 |
|
} else { |
636 |
< |
for (Entry e = header.previous; e != header; e = e.previous) { |
636 |
> |
for (Entry<E> e = header.previous; e != header; e = e.previous) { |
637 |
|
if (o.equals(e.element)) { |
638 |
|
remove(e); |
639 |
|
return true; |
658 |
|
* time in the future. |
659 |
|
* |
660 |
|
* @param index index of the first element to be returned from the |
661 |
< |
* list-iterator (by a call to <tt>next</tt>). |
661 |
> |
* list-iterator (by a call to <tt>next</tt>) |
662 |
|
* @return a ListIterator of the elements in this list (in proper |
663 |
< |
* sequence), starting at the specified position in the list. |
664 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
666 |
< |
* (<tt>index < 0 || index > size()</tt>). |
663 |
> |
* sequence), starting at the specified position in the list |
664 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
665 |
|
* @see List#listIterator(int) |
666 |
|
*/ |
667 |
|
public ListIterator<E> listIterator(int index) { |
827 |
|
|
828 |
|
/** |
829 |
|
* Returns an array containing all of the elements in this list |
830 |
< |
* in the correct order. |
830 |
> |
* in proper sequence (from first to last element). |
831 |
> |
* |
832 |
> |
* <p>The returned array will be "safe" in that no references to it are |
833 |
> |
* maintained by this list. (In other words, this method must allocate |
834 |
> |
* a new array). The caller is thus free to modify the returned array. |
835 |
> |
* |
836 |
> |
* <p>This method acts as bridge between array-based and collection-based |
837 |
> |
* APIs. |
838 |
|
* |
839 |
|
* @return an array containing all of the elements in this list |
840 |
< |
* in the correct order. |
840 |
> |
* in proper sequence |
841 |
|
*/ |
842 |
|
public Object[] toArray() { |
843 |
|
Object[] result = new Object[size]; |
849 |
|
|
850 |
|
/** |
851 |
|
* Returns an array containing all of the elements in this list in |
852 |
< |
* the correct order; the runtime type of the returned array is that of |
853 |
< |
* the specified array. If the list fits in the specified array, it |
854 |
< |
* is returned therein. Otherwise, a new array is allocated with the |
855 |
< |
* runtime type of the specified array and the size of this list.<p> |
856 |
< |
* |
857 |
< |
* If the list fits in the specified array with room to spare |
858 |
< |
* (i.e., the array has more elements than the list), |
859 |
< |
* the element in the array immediately following the end of the |
860 |
< |
* collection is set to null. This is useful in determining the length |
861 |
< |
* of the list <i>only</i> if the caller knows that the list |
862 |
< |
* does not contain any null elements. |
852 |
> |
* proper sequence (from first to last element); the runtime type of |
853 |
> |
* the returned array is that of the specified array. If the list fits |
854 |
> |
* in the specified array, it is returned therein. Otherwise, a new |
855 |
> |
* array is allocated with the runtime type of the specified array and |
856 |
> |
* the size of this list. |
857 |
> |
* |
858 |
> |
* <p>If the list fits in the specified array with room to spare (i.e., |
859 |
> |
* the array has more elements than the list), the element in the array |
860 |
> |
* immediately following the end of the list is set to <tt>null</tt>. |
861 |
> |
* (This is useful in determining the length of the list <i>only</i> if |
862 |
> |
* the caller knows that the list does not contain any null elements.) |
863 |
> |
* |
864 |
> |
* <p>Like the {@link #toArray()} method, this method acts as bridge between |
865 |
> |
* array-based and collection-based APIs. Further, this method allows |
866 |
> |
* precise control over the runtime type of the output array, and may, |
867 |
> |
* under certain circumstances, be used to save allocation costs. |
868 |
> |
* |
869 |
> |
* <p>Suppose <tt>x</tt> is a list known to contain only strings. |
870 |
> |
* The following code can be used to dump the list into a newly |
871 |
> |
* allocated array of <tt>String</tt>: |
872 |
> |
* |
873 |
> |
* <pre> |
874 |
> |
* String[] y = x.toArray(new String[0]);</pre> |
875 |
> |
* |
876 |
> |
* Note that <tt>toArray(new Object[0])</tt> is identical in function to |
877 |
> |
* <tt>toArray()</tt>. |
878 |
|
* |
879 |
|
* @param a the array into which the elements of the list are to |
880 |
|
* be stored, if it is big enough; otherwise, a new array of the |
881 |
|
* same runtime type is allocated for this purpose. |
882 |
|
* @return an array containing the elements of the list |
883 |
< |
* @throws ArrayStoreException if the runtime type of a is not a |
884 |
< |
* supertype of the runtime type of every element in this list |
883 |
> |
* @throws ArrayStoreException if the runtime type of the specified array |
884 |
> |
* is not a supertype of the runtime type of every element in |
885 |
> |
* this list |
886 |
|
* @throws NullPointerException if the specified array is null |
887 |
|
*/ |
888 |
|
public <T> T[] toArray(T[] a) { |