1 使用方法
LinkedList基于双端链表实现,可以作为栈、队列或者双端队列使用。
public class LinkedList<E>
extends AbstractSequentialList<E>
implements List<E>, Deque<E>, Cloneable, java.io.Serializable
LinkedList继承了AbstractSequentialList,实现了get等方法;
LinkedList实现了Deque接口,可以作为双端队列使用;
LinkedList实现Cloneable接口重写了接口定义的clone()方法,可以使用clone()复制链表。
LinkedList实现 Java.io.Serializable接口使LinkedList支持序列化。
1.1 方法介绍
LinkedList提供了增加,弹出,获取元素等操作,具体提供的方法如下:
boolean add(E object)
void add(int location, E object)
boolean addAll(Collection<? extends E> collection)
boolean addAll(int location, Collection<? extends E> collection)
void addFirst(E object)
void addLast(E object)
void clear()
Object clone()
boolean contains(Object object)
Iterator<E> descendingIterator()
E element()
E get(int location)
E getFirst()
E getLast()
int indexOf(Object object)
int lastIndexOf(Object object)
ListIterator<E> listIterator(int location)
boolean offer(E o)
boolean offerFirst(E e)
boolean offerLast(E e)
E peek()
E peekFirst()
E peekLast()
E poll()
E pollFirst()
E pollLast()
E pop()
void push(E e)
E remove()
E remove(int location)
boolean remove(Object object)
E removeFirst()
boolean removeFirstOccurrence(Object o)
E removeLast()
boolean removeLastOccurrence(Object o)
E set(int location, E object)
int size()
<T> T[] toArray(T[] contents)
Object[] toArray()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
1.2 使用示例
public class TestLinkedList {
public void testLinkedList() throws Exception {
LinkedList<String> linkedList = new LinkedList<String>()
linkedList.add("a")
linkedList.add("b")
printLinkedList(linkedList)
linkedList.addFirst("pre-a")
System.out.println("链表中包含 'a' 元素:" + linkedList.contains("a"))
System.out.println("链表的第一个元素: " + linkedList.peek())
System.out.println("链表的最后一个元素: " + linkedList.peekLast())
printLinkedList(linkedList)
System.out.println("获取删除链表的第一个元素: " + linkedList.poll())
printLinkedList(linkedList)
System.out.println("获取并弹出链表的最后一个元素" + linkedList.pollLast())
printLinkedList(linkedList)
linkedList.offer("d")
linkedList.offer("e")
linkedList.offer("f")
printLinkedList(linkedList)
System.out.println("第三个元素为: " + linkedList.get(2))
System.out.println("将第四个元素设置为g: " + linkedList.set(3, "g"))
printLinkedList(linkedList)
//转换成数组
String[] arr = (String[]) linkedList.toArray(new String[0])
for (String e: arr) {
System.out.print(e + " ")
}
}
protected void printLinkedList(LinkedList<String> linkedList) {
Iterator<String> iterator = linkedList.iterator()
System.out.print("linkList包含的元素有: ")
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ")
}
System.out.println("\n")
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
运行结果如下:
linkList包含的元素有: a b
链表中包含 'a' 元素:true
链表的第一个元素: pre-a
链表的最后一个元素: b
linkList包含的元素有: pre-a a b
获取删除链表的第一个元素: pre-a
linkList包含的元素有: a b
获取并弹出链表的最后一个元素b
linkList包含的元素有: a
linkList包含的元素有: a d e f
第三个元素为: e
将第四个元素设置为g: f
linkList包含的元素有: a d e g
a d e g
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2 源码分析
2.1 add方法
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
2.2 addFirst方法
public void addFirst(E e) {
linkFirst(e);
}
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
size++;
modCount++;
}
2.3 peek方法
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
2.4 poll方法
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
private E unlinkFirst(Node<E> f) {
final E element = f.item;
final Node<E> next = f.next;
f.item = null;
f.next = null;
first = next;
if (next == null)
last = null;
else
next.prev = null;
size--;
modCount++;
return element;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2.5 toArray(T[] contents) 方法
public <T> T[] toArray(T[] a) {
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
int i = 0;
Object[] result = a;
for (Node<E> x = first; x != null; x = x.next)
result[i++] = x.item;
if (a.length > size)
a[size] = null;
return a;
}
参考:
[1] http://blog.csdn.net/crave_shy/article/details/17440835
[2] http://blog.csdn.net/wanghao109/article/details/13287877
[3] http://fjohnny.iteye.com/blog/696750