1 |
|
/* |
2 |
|
* %W% %E% |
3 |
|
* |
4 |
< |
* Copyright 2005 Sun Microsystems, Inc. All rights reserved. |
4 |
> |
* Copyright 2007 Sun Microsystems, Inc. All rights reserved. |
5 |
|
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. |
6 |
|
*/ |
7 |
|
|
25 |
|
* list. Operations that index into the list will traverse the list from |
26 |
|
* the beginning or the end, whichever is closer to the specified index.<p> |
27 |
|
* |
28 |
< |
* <b>Note that this implementation is not synchronized.</b> If multiple |
29 |
< |
* threads access a list concurrently, and at least one of the threads |
30 |
< |
* modifies the list structurally, it <i>must</i> be synchronized |
31 |
< |
* externally. (A structural modification is any operation that adds or |
32 |
< |
* deletes one or more elements; merely setting the value of an element is not |
33 |
< |
* a structural modification.) This is typically accomplished by |
34 |
< |
* synchronizing on some object that naturally encapsulates the list. If no |
35 |
< |
* such object exists, the list should be "wrapped" using the |
36 |
< |
* Collections.synchronizedList method. This is best done at creation time, |
37 |
< |
* to prevent accidental unsynchronized access to the list: <pre> |
38 |
< |
* List list = Collections.synchronizedList(new LinkedList(...)); |
39 |
< |
* </pre> |
28 |
> |
* <p><strong>Note that this implementation is not synchronized.</strong> |
29 |
> |
* If multiple threads access a linked list concurrently, and at least |
30 |
> |
* one of the threads modifies the list structurally, it <i>must</i> be |
31 |
> |
* synchronized externally. (A structural modification is any operation |
32 |
> |
* that adds or deletes one or more elements; merely setting the value of |
33 |
> |
* an element is not a structural modification.) This is typically |
34 |
> |
* accomplished by synchronizing on some object that naturally |
35 |
> |
* encapsulates the list. |
36 |
> |
* |
37 |
> |
* If no such object exists, the list should be "wrapped" using the |
38 |
> |
* {@link Collections#synchronizedList Collections.synchronizedList} |
39 |
> |
* method. This is best done at creation time, to prevent accidental |
40 |
> |
* unsynchronized access to the list:<pre> |
41 |
> |
* List list = Collections.synchronizedList(new LinkedList(...));</pre> |
42 |
|
* |
43 |
|
* <p>The iterators returned by this class's <tt>iterator</tt> and |
44 |
|
* <tt>listIterator</tt> methods are <i>fail-fast</i>: if the list is |
59 |
|
* should be used only to detect bugs.</i> |
60 |
|
* |
61 |
|
* <p>This class is a member of the |
62 |
< |
* <a href="{@docRoot}/../guide/collections/index.html"> |
62 |
> |
* <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
63 |
|
* Java Collections Framework</a>. |
64 |
|
* |
65 |
|
* @author Josh Bloch |
67 |
|
* @see List |
68 |
|
* @see ArrayList |
69 |
|
* @see Vector |
68 |
– |
* @see Collections#synchronizedList(List) |
70 |
|
* @since 1.2 |
71 |
|
* @param <E> the type of elements held in this collection |
72 |
|
*/ |
93 |
|
* @param c the collection whose elements are to be placed into this list |
94 |
|
* @throws NullPointerException if the specified collection is null |
95 |
|
*/ |
96 |
< |
public LinkedList(Collection<? extends E> c) { |
97 |
< |
this(); |
98 |
< |
addAll(c); |
99 |
< |
} |
96 |
> |
public LinkedList(Collection<? extends E> c) { |
97 |
> |
this(); |
98 |
> |
addAll(c); |
99 |
> |
} |
100 |
|
|
101 |
|
/** |
102 |
|
* Returns the first element in this list. |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* Inserts the given element at the beginning of this list. |
148 |
> |
* Inserts the specified element at the beginning of this list. |
149 |
|
* |
150 |
< |
* @param e the element to be inserted at the beginning of this list |
150 |
> |
* @param e the element to add |
151 |
|
*/ |
152 |
|
public void addFirst(E e) { |
153 |
|
addBefore(e, header.next); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* Appends the given element to the end of this list. (Identical in |
157 |
< |
* function to the <tt>add</tt> method; included only for consistency.) |
157 |
> |
* Appends the specified element to the end of this list. |
158 |
|
* |
159 |
< |
* @param e the element to be inserted at the end of this list |
159 |
> |
* <p>This method is equivalent to {@link #add}. |
160 |
> |
* |
161 |
> |
* @param e the element to add |
162 |
|
*/ |
163 |
|
public void addLast(E e) { |
164 |
|
addBefore(e, header); |
189 |
|
/** |
190 |
|
* Appends the specified element to the end of this list. |
191 |
|
* |
192 |
+ |
* <p>This method is equivalent to {@link #addLast}. |
193 |
+ |
* |
194 |
|
* @param e element to be appended to this list |
195 |
< |
* @return <tt>true</tt> (as per the spec for {@link Collection#add}) |
195 |
> |
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
196 |
|
*/ |
197 |
|
public boolean add(E e) { |
198 |
|
addBefore(e, header); |
236 |
|
* this list, in the order that they are returned by the specified |
237 |
|
* collection's iterator. The behavior of this operation is undefined if |
238 |
|
* the specified collection is modified while the operation is in |
239 |
< |
* progress. (This implies that the behavior of this call is undefined if |
240 |
< |
* the specified Collection is this list, and this list is nonempty.) |
239 |
> |
* progress. (Note that this will occur if the specified collection is |
240 |
> |
* this list, and it's nonempty.) |
241 |
|
* |
242 |
< |
* @param c the elements to be inserted into this list |
242 |
> |
* @param c collection containing elements to be added to this list |
243 |
|
* @return <tt>true</tt> if this list changed as a result of the call |
244 |
|
* @throws NullPointerException if the specified collection is null |
245 |
|
*/ |
257 |
|
* |
258 |
|
* @param index index at which to insert the first element |
259 |
|
* from the specified collection |
260 |
< |
* @param c elements to be inserted into this list |
260 |
> |
* @param c collection containing elements to be added to this list |
261 |
|
* @return <tt>true</tt> if this list changed as a result of the call |
262 |
|
* @throws IndexOutOfBoundsException {@inheritDoc} |
263 |
|
* @throws NullPointerException if the specified collection is null |
485 |
|
* Adds the specified element as the tail (last element) of this list. |
486 |
|
* |
487 |
|
* @param e the element to add |
488 |
< |
* @return <tt>true</tt> (as per the spec for {@link Queue#offer}) |
488 |
> |
* @return <tt>true</tt> (as specified by {@link Queue#offer}) |
489 |
|
* @since 1.5 |
490 |
|
*/ |
491 |
|
public boolean offer(E e) { |
497 |
|
* Inserts the specified element at the front of this list. |
498 |
|
* |
499 |
|
* @param e the element to insert |
500 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst}) |
500 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerFirst}) |
501 |
|
* @since 1.6 |
502 |
|
*/ |
503 |
|
public boolean offerFirst(E e) { |
509 |
|
* Inserts the specified element at the end of this list. |
510 |
|
* |
511 |
|
* @param e the element to insert |
512 |
< |
* @return <tt>true</tt> (as per the spec for {@link Deque#offerLast}) |
512 |
> |
* @return <tt>true</tt> (as specified by {@link Deque#offerLast}) |
513 |
|
* @since 1.6 |
514 |
|
*/ |
515 |
|
public boolean offerLast(E e) { |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
< |
* Retrieves and removes the first element of this list, or |
550 |
< |
* <tt>null</tt> if this list is empty. |
549 |
> |
* Retrieves and removes the first element of this list, |
550 |
> |
* or returns <tt>null</tt> if this list is empty. |
551 |
|
* |
552 |
|
* @return the first element of this list, or <tt>null</tt> if |
553 |
|
* this list is empty |
560 |
|
} |
561 |
|
|
562 |
|
/** |
563 |
< |
* Retrieves and removes the last element of this list, or |
564 |
< |
* <tt>null</tt> if this list is empty. |
563 |
> |
* Retrieves and removes the last element of this list, |
564 |
> |
* or returns <tt>null</tt> if this list is empty. |
565 |
|
* |
566 |
|
* @return the last element of this list, or <tt>null</tt> if |
567 |
|
* this list is empty |
625 |
|
*/ |
626 |
|
public boolean removeLastOccurrence(Object o) { |
627 |
|
if (o==null) { |
628 |
< |
for (Entry e = header.previous; e != header; e = e.previous) { |
628 |
> |
for (Entry<E> e = header.previous; e != header; e = e.previous) { |
629 |
|
if (e.element==null) { |
630 |
|
remove(e); |
631 |
|
return true; |
632 |
|
} |
633 |
|
} |
634 |
|
} else { |
635 |
< |
for (Entry e = header.previous; e != header; e = e.previous) { |
635 |
> |
for (Entry<E> e = header.previous; e != header; e = e.previous) { |
636 |
|
if (o.equals(e.element)) { |
637 |
|
remove(e); |
638 |
|
return true; |
798 |
|
} |
799 |
|
|
800 |
|
/** |
801 |
+ |
* @since 1.6 |
802 |
+ |
*/ |
803 |
+ |
public Iterator<E> descendingIterator() { |
804 |
+ |
return new DescendingIterator(); |
805 |
+ |
} |
806 |
+ |
|
807 |
+ |
/** Adapter to provide descending iterators via ListItr.previous */ |
808 |
+ |
private class DescendingIterator implements Iterator { |
809 |
+ |
final ListItr itr = new ListItr(size()); |
810 |
+ |
public boolean hasNext() { |
811 |
+ |
return itr.hasPrevious(); |
812 |
+ |
} |
813 |
+ |
public E next() { |
814 |
+ |
return itr.previous(); |
815 |
+ |
} |
816 |
+ |
public void remove() { |
817 |
+ |
itr.remove(); |
818 |
+ |
} |
819 |
+ |
} |
820 |
+ |
|
821 |
+ |
/** |
822 |
|
* Returns a shallow copy of this <tt>LinkedList</tt>. (The elements |
823 |
|
* themselves are not cloned.) |
824 |
|
* |
852 |
|
* <p>The returned array will be "safe" in that no references to it are |
853 |
|
* maintained by this list. (In other words, this method must allocate |
854 |
|
* a new array). The caller is thus free to modify the returned array. |
855 |
< |
* |
855 |
> |
* |
856 |
|
* <p>This method acts as bridge between array-based and collection-based |
857 |
|
* APIs. |
858 |
|
* |