LinkedList类
目录
All Implemented Interfaces(所有已实现接口)
所在包:java.util.LinkedList
- Type Parameters(参数类型):
E
- the type of elements held in this collection
- All Implemented Interfaces(所有已实现接口):
- Serializable, Cloneable, Iterable<E>, Collection<E>, Deque<E>, List<E>, Queue<E>
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, Serializable
List
and Deque
interfaces.null
).双链表实现了List和Deque接口。
实现所有可选的列表操作,并允许所有元素(包括null)。
All of the operations perform as could be expected for a doubly-linked list.
Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
所有操作的执行都符合双链表的预期。
索引到列表中的操作将从头或尾遍历列表,以更接近指定索引的操作为准。
Note that this implementation is not synchronized. If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the Collections.synchronizedList
method. This is best done at creation time, to prevent accidental unsynchronized access to the list:
注意,这个实现不是同步的。如果多个线程同时访问一个链表,并且至少有一个线程从结构上修改了链表,那么它必须在外部同步。(结构修改是指增加或删除一个或多个元素的任何操作;仅仅设置元素的值并不是一种结构修改。)这通常是通过对一些自然封装列表的对象进行同步来实现的。如果不存在这样的对象,则应该使用集合对列表进行“包装”。synchronizedList方法。这最好在创建时完成,以防止意外的不同步访问列表:
List list = Collections.synchronizedList(new LinkedList(...));
The iterators returned by this class's iterator
and listIterator
methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove
or add
methods, the iterator will throw a ConcurrentModificationException
. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
这个类的iterator和listIterator方法返回的迭代器是快速失效的:如果在创建迭代器之后的任何时候,以任何方式(除了通过迭代器自己的删除或添加方法)对列表进行结构修改,迭代器将抛出ConcurrentModificationException异常。因此,在面对并发修改时,迭代器会快速而干净地失败,而不是在将来某个不确定的时间冒任意的、不确定的行为的风险。
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException
on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.
注意,不能保证迭代器的快速故障行为,因为通常来说,在存在非同步并发修改的情况下,不可能做出任何严格的保证。故障快速迭代器在最大努力的基础上抛出ConcurrentModificationException。因此,编写一个依赖于这个异常的正确性的程序是错误的:迭代器的快速故障行为应该只用于检测bug。
This class is a member of the Java Collections Framework.
该类是Java集合框架的成员。
- Since:
- 1.2
- See Also:
List
,ArrayList
, Serialized Form
Field Summary(字段汇总)
-
Fields inherited from class java.util.AbstractList
modCount
Constructor Summary(构造函数的总结)
Constructor and Description 构造函数和描述 |
---|
LinkedList()
Constructs an empty list.
构造一个空的list列表。
|
LinkedList(Collection<? extends E> c)
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.
构造一个包含指定集合元素的list列表,按集合的迭代器返回元素的顺序排列。
|
Method Summary(方法总结)
Modifier and Type 修饰符和类型 | Method and Description 方法和描述 |
---|---|
boolean |
add(E e)
Appends the specified element to the end of this list.
将指定的元素附加到此列表的末尾。
|
void |
add(int index, E element)
Inserts the specified element at the specified position in this list.
将指定元素插入到列表中的指定位置。 |
boolean |
addAll(Collection<? extends E> c)
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
将指定集合中的所有元素附加到该列表的末尾,顺序是由指定的集合的迭代器返回。 |
boolean |
addAll(int index, Collection<? extends E> c)
Inserts all of the elements in the specified collection into this list, starting at the specified position.
从指定位置开始,将指定集合中的所有元素插入此列表。 |
void |
addFirst(E e)
Inserts the specified element at the beginning of this list.
将指定的元素插入此列表的开头。 |
void |
addLast(E e)
Appends the specified element to the end of this list.
将指定的元素附加到此列表的末尾。 |
void |
clear()
Removes all of the elements from this list.
从列表中删除所有元素。 |
Object |
clone()
Returns a shallow copy of this
LinkedList .返回该链表的浅拷贝。 |
boolean |
contains(Object o)
Returns
true if this list contains the specified element.如果此列表包含指定的元素,则返回true。 |
Iterator<E> |
descendingIterator()
Returns an iterator over the elements in this deque in reverse sequential order.
以相反的顺序返回deque中元素的迭代器。 |
E |
element()
Retrieves, but does not remove, the head (first element) of this list.
检索但不删除此列表的头(第一个元素)。 |
E |
get(int index)
Returns the element at the specified position in this list.
返回此列表中指定位置的元素。 |
E |
getFirst()
Returns the first element in this list.
返回列表中的第一个元素。 |
E |
getLast()
Returns the last element in this list.
返回列表中的最后一个元素。
|
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
返回此列表中指定元素的第一个匹配项的索引,如果此列表不包含该元素,则返回-1。 |
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
返回此列表中指定元素的最后一次出现的索引,如果此列表不包含该元素,则返回-1。 |
ListIterator<E> |
listIterator(int index)
Returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
返回此列表中元素的列表迭代器(按适当的顺序),从列表中的指定位置开始。 |
boolean |
offer(E e)
Adds the specified element as the tail (last element) of this list.
将指定的元素添加为此列表的末尾(最后一个元素)。 |
boolean |
offerFirst(E e)
Inserts the specified element at the front of this list.
将指定的元素插入此列表的前面。 |
boolean |
offerLast(E e)
Inserts the specified element at the end of this list.
在列表末尾插入指定的元素。 |
E |
peek()
Retrieves, but does not remove, the head (first element) of this list.
检索但不删除此列表的头(第一个元素)。 |
E |
peekFirst()
Retrieves, but does not remove, the first element of this list, or returns
null if this list is empty.检索但不删除此列表的第一个元素,或在此列表为空时返回null。 |
E |
peekLast()
Retrieves, but does not remove, the last element of this list, or returns
null if this list is empty.检索但不删除此列表的最后一个元素,或在此列表为空时返回null。 |
E |
poll()
Retrieves and removes the head (first element) of this list.
检索并删除此列表的头(第一个元素)。
|
E |
pollFirst()
Retrieves and removes the first element of this list, or returns
null if this list is empty.检索并删除此列表的第一个元素,如果该列表为空,则返回null。 |
E |
pollLast()
Retrieves and removes the last element of this list, or returns
null if this list is empty.检索并删除此列表的最后一个元素,如果此列表为空,则返回null。
|
E |
pop()
Pops an element from the stack represented by this list.
从该列表表示的堆栈中弹出一个元素。 |
void |
push(E e)
Pushes an element onto the stack represented by this list.
将元素推入此列表所表示的堆栈。 |
E |
remove()
Retrieves and removes the head (first element) of this list.
检索并删除此列表的头(第一个元素)。 |
E |
remove(int index)
Removes the element at the specified position in this list.
删除列表中指定位置的元素。 |
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present.
从该列表中删除指定元素的第一个匹配项(如果存在)。 |
E |
removeFirst()
Removes and returns the first element from this list.
从列表中删除并返回第一个元素。 |
boolean |
removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail).
删除此列表中指定元素的第一个匹配项(在从头到尾遍历该列表时)。 |
E |
removeLast()
Removes and returns the last element from this list.
从列表中删除并返回最后一个元素。 |
boolean |
removeLastOccurrence(Object o)
Removes the last occurrence of the specified element in this list (when traversing the list from head to tail).
删除此列表中指定元素的最后一次出现(在从头到尾遍历该列表时)。 |
E |
set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
将列表中指定位置的元素替换为指定元素。 |
int |
size()
Returns the number of elements in this list.
返回列表中元素的数目。 |
Spliterator<E> |
spliterator()
Creates a late-binding and fail-fast
Spliterator over the elements in this list.在此列表中的元素上创建延迟绑定和故障快速Spliterator。 |
Object[] |
toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
返回一个数组,该数组按适当的顺序(从第一个元素到最后一个元素)包含列表中的所有元素。 |
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element);
the runtime type of the returned array is that of the specified array. 返回一个数组,该数组按适当的顺序包含列表中的所有元素(从第一个元素到最后一个元素);
返回数组的运行时类型是指定数组的运行时类型。
|
Methods inherited from class java.util.AbstractSequentialList
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subList
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.List
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList
Methods inherited from interface java.util.Deque
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.lang.Iterable