89 |
|
* collection, in the order they are returned by the collection's |
90 |
|
* iterator. |
91 |
|
* |
92 |
< |
* @param c the collection whose elements are to be placed into this list. |
93 |
< |
* @throws NullPointerException if the specified collection is null. |
92 |
> |
* @param c the collection whose elements are to be placed into this list |
93 |
> |
* @throws NullPointerException if the specified collection is null |
94 |
|
*/ |
95 |
|
public LinkedList(Collection<? extends E> c) { |
96 |
|
this(); |
100 |
|
/** |
101 |
|
* Returns the first element in this list. |
102 |
|
* |
103 |
< |
* @return the first element in this list. |
104 |
< |
* @throws NoSuchElementException if this list is empty. |
103 |
> |
* @return the first element in this list |
104 |
> |
* @throws NoSuchElementException if this list is empty |
105 |
|
*/ |
106 |
|
public E getFirst() { |
107 |
|
if (size==0) |
113 |
|
/** |
114 |
|
* Returns the last element in this list. |
115 |
|
* |
116 |
< |
* @return the last element in this list. |
117 |
< |
* @throws NoSuchElementException if this list is empty. |
116 |
> |
* @return the last element in this list |
117 |
> |
* @throws NoSuchElementException if this list is empty |
118 |
|
*/ |
119 |
|
public E getLast() { |
120 |
|
if (size==0) |
126 |
|
/** |
127 |
|
* Removes and returns the first element from this list. |
128 |
|
* |
129 |
< |
* @return the first element from this list. |
130 |
< |
* @throws NoSuchElementException if this list is empty. |
129 |
> |
* @return the first element from this list |
130 |
> |
* @throws NoSuchElementException if this list is empty |
131 |
|
*/ |
132 |
|
public E removeFirst() { |
133 |
|
return remove(header.next); |
136 |
|
/** |
137 |
|
* Removes and returns the last element from this list. |
138 |
|
* |
139 |
< |
* @return the last element from this list. |
140 |
< |
* @throws NoSuchElementException if this list is empty. |
139 |
> |
* @return the last element from this list |
140 |
> |
* @throws NoSuchElementException if this list is empty |
141 |
|
*/ |
142 |
|
public E removeLast() { |
143 |
|
return remove(header.previous); |
146 |
|
/** |
147 |
|
* Inserts the given element at the beginning of this list. |
148 |
|
* |
149 |
< |
* @param e the element to be inserted at the beginning of this list. |
149 |
> |
* @param e the element to be inserted at the beginning of this list |
150 |
|
*/ |
151 |
|
public void addFirst(E e) { |
152 |
|
addBefore(e, header.next); |
156 |
|
* 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 |
|
* |
159 |
< |
* @param e the element to be inserted at the end of this list. |
159 |
> |
* @param e the element to be inserted at the end of this list |
160 |
|
*/ |
161 |
|
public void addLast(E e) { |
162 |
|
addBefore(e, header); |
165 |
|
/** |
166 |
|
* Returns <tt>true</tt> if this list contains the specified element. |
167 |
|
* More formally, returns <tt>true</tt> if and only if this list contains |
168 |
< |
* at least one element <tt>e</tt> such that <tt>(o==null ? e==null |
169 |
< |
* : o.equals(e))</tt>. |
168 |
> |
* at least one element <tt>e</tt> such that |
169 |
> |
* <tt>(o==null ? e==null : o.equals(e))</tt>. |
170 |
|
* |
171 |
< |
* @param o element whose presence in this list is to be tested. |
172 |
< |
* @return <tt>true</tt> if this list contains the specified element. |
171 |
> |
* @param o element whose presence in this list is to be tested |
172 |
> |
* @return <tt>true</tt> if this list contains the specified element |
173 |
|
*/ |
174 |
|
public boolean contains(Object o) { |
175 |
|
return indexOf(o) != -1; |
178 |
|
/** |
179 |
|
* Returns the number of elements in this list. |
180 |
|
* |
181 |
< |
* @return the number of elements in this list. |
181 |
> |
* @return the number of elements in this list |
182 |
|
*/ |
183 |
|
public int size() { |
184 |
|
return size; |
187 |
|
/** |
188 |
|
* Appends the specified element to the end of this list. |
189 |
|
* |
190 |
< |
* @param e element to be appended to this list. |
191 |
< |
* @return <tt>true</tt> (as per the general contract of |
192 |
< |
* <tt>Collection.add</tt>). |
190 |
> |
* @param e element to be appended to this list |
191 |
> |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
192 |
|
*/ |
193 |
|
public boolean add(E e) { |
194 |
|
addBefore(e, header); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
< |
* Removes the first occurrence of the specified element in this list. If |
200 |
< |
* the list does not contain the element, it is unchanged. More formally, |
201 |
< |
* removes the element with the lowest index <tt>i</tt> such that |
202 |
< |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> (if such an |
203 |
< |
* element exists). |
199 |
> |
* Removes the first occurrence of the specified element from this list, |
200 |
> |
* if it is present. If this list does not contain the element, it is |
201 |
> |
* unchanged. More formally, removes the element with the lowest index |
202 |
> |
* <tt>i</tt> such that |
203 |
> |
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt> |
204 |
> |
* (if such an element exists). Returns <tt>true</tt> if this list |
205 |
> |
* contained the specified element (or equivalently, if this list |
206 |
> |
* changed as a result of the call). |
207 |
|
* |
208 |
< |
* @param o element to be removed from this list, if present. |
209 |
< |
* @return <tt>true</tt> if the list contained the specified element. |
208 |
> |
* @param o element to be removed from this list, if present |
209 |
> |
* @return <tt>true</tt> if this list contained the specified element |
210 |
|
*/ |
211 |
|
public boolean remove(Object o) { |
212 |
|
if (o==null) { |
235 |
|
* progress. (This implies that the behavior of this call is undefined if |
236 |
|
* the specified Collection is this list, and this list is nonempty.) |
237 |
|
* |
238 |
< |
* @param c the elements to be inserted into this list. |
239 |
< |
* @return <tt>true</tt> if this list changed as a result of the call. |
240 |
< |
* @throws NullPointerException if the specified collection is null. |
238 |
> |
* @param c the elements to be inserted into this list |
239 |
> |
* @return <tt>true</tt> if this list changed as a result of the call |
240 |
> |
* @throws NullPointerException if the specified collection is null |
241 |
|
*/ |
242 |
|
public boolean addAll(Collection<? extends E> c) { |
243 |
|
return addAll(size, c); |
251 |
|
* in the list in the order that they are returned by the |
252 |
|
* specified collection's iterator. |
253 |
|
* |
254 |
< |
* @param index index at which to insert first element |
255 |
< |
* from the specified collection. |
256 |
< |
* @param c elements to be inserted into this list. |
257 |
< |
* @return <tt>true</tt> if this list changed as a result of the call. |
258 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
259 |
< |
* (<tt>index < 0 || index > size()</tt>). |
258 |
< |
* @throws NullPointerException if the specified collection is null. |
254 |
> |
* @param index index at which to insert the first element |
255 |
> |
* from the specified collection |
256 |
> |
* @param c elements to be inserted into this list |
257 |
> |
* @return <tt>true</tt> if this list changed as a result of the call |
258 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
259 |
> |
* @throws NullPointerException if the specified collection is null |
260 |
|
*/ |
261 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
262 |
|
if (index < 0 || index > size) |
303 |
|
/** |
304 |
|
* Returns the element at the specified position in this list. |
305 |
|
* |
306 |
< |
* @param index index of element to return. |
307 |
< |
* @return the element at the specified position in this list. |
308 |
< |
* |
308 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
309 |
< |
* (<tt>index < 0 || index > size()</tt>). |
306 |
> |
* @param index index of the element to return |
307 |
> |
* @return the element at the specified position in this list |
308 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
309 |
|
*/ |
310 |
|
public E get(int index) { |
311 |
|
return entry(index).element; |
315 |
|
* Replaces the element at the specified position in this list with the |
316 |
|
* specified element. |
317 |
|
* |
318 |
< |
* @param index index of element to replace. |
319 |
< |
* @param element element to be stored at the specified position. |
320 |
< |
* @return the element previously at the specified position. |
321 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
323 |
< |
* (<tt>index < 0 || index > size()</tt>). |
318 |
> |
* @param index index of the element to replace |
319 |
> |
* @param element element to be stored at the specified position |
320 |
> |
* @return the element previously at the specified position |
321 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
322 |
|
*/ |
323 |
|
public E set(int index, E element) { |
324 |
|
Entry<E> e = entry(index); |
332 |
|
* Shifts the element currently at that position (if any) and any |
333 |
|
* subsequent elements to the right (adds one to their indices). |
334 |
|
* |
335 |
< |
* @param index index at which the specified element is to be inserted. |
336 |
< |
* @param element element to be inserted. |
337 |
< |
* |
340 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
341 |
< |
* (<tt>index < 0 || index > size()</tt>). |
335 |
> |
* @param index index at which the specified element is to be inserted |
336 |
> |
* @param element element to be inserted |
337 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
338 |
|
*/ |
339 |
|
public void add(int index, E element) { |
340 |
|
addBefore(element, (index==size ? header : entry(index))); |
345 |
|
* subsequent elements to the left (subtracts one from their indices). |
346 |
|
* Returns the element that was removed from the list. |
347 |
|
* |
348 |
< |
* @param index the index of the element to removed. |
349 |
< |
* @return the element previously at the specified position. |
350 |
< |
* |
355 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
356 |
< |
* (<tt>index < 0 || index > size()</tt>). |
348 |
> |
* @param index the index of the element to be removed |
349 |
> |
* @return the element previously at the specified position |
350 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
351 |
|
*/ |
352 |
|
public E remove(int index) { |
353 |
|
return remove(entry(index)); |
381 |
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
382 |
|
* there is no such index. |
383 |
|
* |
384 |
< |
* @param o element to search for. |
384 |
> |
* @param o element to search for |
385 |
|
* @return the index in this list of the first occurrence of the |
386 |
< |
* specified element, or -1 if the list does not contain this |
387 |
< |
* element. |
386 |
> |
* specified element, or -1 if the list does not contain this |
387 |
> |
* element |
388 |
|
*/ |
389 |
|
public int indexOf(Object o) { |
390 |
|
int index = 0; |
411 |
|
* <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, or -1 if |
412 |
|
* there is no such index. |
413 |
|
* |
414 |
< |
* @param o element to search for. |
414 |
> |
* @param o element to search for |
415 |
|
* @return the index in this list of the last occurrence of the |
416 |
< |
* specified element, or -1 if the list does not contain this |
417 |
< |
* element. |
416 |
> |
* specified element, or -1 if the list does not contain this |
417 |
> |
* element |
418 |
|
*/ |
419 |
|
public int lastIndexOf(Object o) { |
420 |
|
int index = size; |
438 |
|
|
439 |
|
/** |
440 |
|
* Retrieves, but does not remove, the head (first element) of this list. |
441 |
< |
* @return the head of this list, or <tt>null</tt> if this list is empty. |
441 |
> |
* @return the head of this list, or <tt>null</tt> if this list is empty |
442 |
|
* @since 1.5 |
443 |
|
*/ |
444 |
|
public E peek() { |
449 |
|
|
450 |
|
/** |
451 |
|
* Retrieves, but does not remove, the head (first element) of this list. |
452 |
< |
* @return the head of this list. |
453 |
< |
* @throws NoSuchElementException if this list is empty. |
452 |
> |
* @return the head of this list |
453 |
> |
* @throws NoSuchElementException if this list is empty |
454 |
|
* @since 1.5 |
455 |
|
*/ |
456 |
|
public E element() { |
458 |
|
} |
459 |
|
|
460 |
|
/** |
461 |
< |
* Retrieves and removes the head (first element) of this list. |
462 |
< |
* @return the head of this list, or <tt>null</tt> if this list is empty. |
461 |
> |
* Retrieves and removes the head (first element) of this list |
462 |
> |
* @return the head of this list, or <tt>null</tt> if this list is empty |
463 |
|
* @since 1.5 |
464 |
|
*/ |
465 |
|
public E poll() { |
470 |
|
|
471 |
|
/** |
472 |
|
* Retrieves and removes the head (first element) of this list. |
473 |
< |
* @return the head of this list. |
474 |
< |
* @throws NoSuchElementException if this list is empty. |
473 |
> |
* |
474 |
> |
* @return the head of this list |
475 |
> |
* @throws NoSuchElementException if this list is empty |
476 |
|
* @since 1.5 |
477 |
|
*/ |
478 |
|
public E remove() { |
482 |
|
/** |
483 |
|
* Adds the specified element as the tail (last element) of this list. |
484 |
|
* |
485 |
< |
* @param e the element to add. |
486 |
< |
* @return <tt>true</tt> (as per the general contract of |
492 |
< |
* <tt>Queue.offer</tt>) |
485 |
> |
* @param e the element to add |
486 |
> |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
487 |
|
* @since 1.5 |
488 |
|
*/ |
489 |
|
public boolean offer(E e) { |
517 |
|
|
518 |
|
/** |
519 |
|
* Retrieves, but does not remove, the first element of this list, |
520 |
< |
* returning <tt>null</tt> if this list is empty. |
520 |
> |
* or returns <tt>null</tt> if this list is empty. |
521 |
|
* |
522 |
< |
* @return the first element of this list, or <tt>null</tt> if |
523 |
< |
* this list is empty |
522 |
> |
* @return the first element of this list, or <tt>null</tt> |
523 |
> |
* if this list is empty |
524 |
|
* @since 1.6 |
525 |
|
*/ |
526 |
|
public E peekFirst() { |
531 |
|
|
532 |
|
/** |
533 |
|
* Retrieves, but does not remove, the last element of this list, |
534 |
< |
* returning <tt>null</tt> if this list is empty. |
534 |
> |
* or returns <tt>null</tt> if this list is empty. |
535 |
|
* |
536 |
< |
* @return the last element of this list, or <tt>null</tt> if this list |
537 |
< |
* is empty |
536 |
> |
* @return the last element of this list, or <tt>null</tt> |
537 |
> |
* if this list is empty |
538 |
|
* @since 1.6 |
539 |
|
*/ |
540 |
|
public E peekLast() { |
591 |
|
* <p>This method is equivalent to {@link #removeFirst()}. |
592 |
|
* |
593 |
|
* @return the element at the front of this list (which is the top |
594 |
< |
* of the stack represented by this list) |
595 |
< |
* @throws NoSuchElementException if this list is empty. |
594 |
> |
* of the stack represented by this list) |
595 |
> |
* @throws NoSuchElementException if this list is empty |
596 |
|
* @since 1.6 |
597 |
|
*/ |
598 |
|
public E pop() { |
654 |
|
* than risking arbitrary, non-deterministic behavior at an undetermined |
655 |
|
* time in the future. |
656 |
|
* |
657 |
< |
* @param index index of first element to be returned from the |
658 |
< |
* list-iterator (by a call to <tt>next</tt>). |
657 |
> |
* @param index index of the first element to be returned from the |
658 |
> |
* list-iterator (by a call to <tt>next</tt>) |
659 |
|
* @return a ListIterator of the elements in this list (in proper |
660 |
< |
* sequence), starting at the specified position in the list. |
661 |
< |
* @throws IndexOutOfBoundsException if the index is out of range |
668 |
< |
* (<tt>index < 0 || index > size()</tt>). |
660 |
> |
* sequence), starting at the specified position in the list |
661 |
> |
* @throws IndexOutOfBoundsException {@inheritDoc} |
662 |
|
* @see List#listIterator(int) |
663 |
|
*/ |
664 |
|
public ListIterator<E> listIterator(int index) { |
799 |
|
* Returns a shallow copy of this <tt>LinkedList</tt>. (The elements |
800 |
|
* themselves are not cloned.) |
801 |
|
* |
802 |
< |
* @return a shallow copy of this <tt>LinkedList</tt> instance. |
802 |
> |
* @return a shallow copy of this <tt>LinkedList</tt> instance |
803 |
|
*/ |
804 |
|
public Object clone() { |
805 |
|
LinkedList<E> clone = null; |
827 |
|
* in the correct order. |
828 |
|
* |
829 |
|
* @return an array containing all of the elements in this list |
830 |
< |
* in the correct order. |
830 |
> |
* in the correct order |
831 |
|
*/ |
832 |
|
public Object[] toArray() { |
833 |
|
Object[] result = new Object[size]; |
852 |
|
* does not contain any null elements. |
853 |
|
* |
854 |
|
* @param a the array into which the elements of the list are to |
855 |
< |
* be stored, if it is big enough; otherwise, a new array of the |
856 |
< |
* same runtime type is allocated for this purpose. |
857 |
< |
* @return an array containing the elements of the list. |
855 |
> |
* be stored, if it is big enough; otherwise, a new array of the |
856 |
> |
* same runtime type is allocated for this purpose. |
857 |
> |
* @return an array containing the elements of the list |
858 |
|
* @throws ArrayStoreException if the runtime type of a is not a |
859 |
< |
* supertype of the runtime type of every element in this list. |
860 |
< |
* @throws NullPointerException if the specified array is null. |
859 |
> |
* supertype of the runtime type of every element in this list |
860 |
> |
* @throws NullPointerException if the specified array is null |
861 |
|
*/ |
862 |
|
public <T> T[] toArray(T[] a) { |
863 |
|
if (a.length < size) |
881 |
|
* is, serialize it). |
882 |
|
* |
883 |
|
* @serialData The size of the list (the number of elements it |
884 |
< |
* contains) is emitted (int), followed by all of its |
885 |
< |
* elements (each an Object) in the proper order. |
884 |
> |
* contains) is emitted (int), followed by all of its |
885 |
> |
* elements (each an Object) in the proper order. |
886 |
|
*/ |
887 |
|
private void writeObject(java.io.ObjectOutputStream s) |
888 |
|
throws java.io.IOException { |