publicclassTest1{ publicstaticvoidmain(String[] args){ List<Integer> arraylist = new ArrayList<>(); arraylist.add(1); arraylist.add(2);
List<Integer> list1 = new LinkedList<>(arraylist); System.out.println(list1); } }
主要方法
add(E e)
末尾插入一个节点。
1 2 3 4
publicbooleanadd(E e){ linkLast(e); returntrue; }
直接调用**linkLast(e)**方法。
1 2 3 4 5 6 7 8 9 10 11
voidlinkLast(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++; }
添加第一个节点过程如下
插入第二个节点
从这可以看出LinkedList是一个双向链表。
add(int index, E element)
指定位置插入一个节点。
1 2 3 4 5 6 7 8
voidadd(int index, E element){ checkPositionIndex(index);
if (index == size) linkLast(element); else linkBefore(element, node(index)); }
调用checkPositionIndex函数,检查index是否符合规范。
1 2 3 4 5 6 7 8
privatebooleanisPositionIndex(int index){ return index >= 0 && index <= size; }
privatevoidcheckPositionIndex(int index){ if (!isPositionIndex(index)) thrownew IndexOutOfBoundsException(outOfBoundsMsg(index)); }
voidlinkBefore(E e, Node<E> succ){ // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; }
private E unlinkFirst(Node<E> f){ // assert f == first && f != null; final E element = f.item; final Node<E> next = f.next; f.item = null; f.next = null; // help GC first = next; if (next == null) last = null; else next.prev = null; size--; modCount++; return element; }
private E unlinkLast(Node<E> l){ // assert l == last && l != null; final E element = l.item; final Node<E> prev = l.prev; l.item = null; l.prev = null; // help GC last = prev; if (prev == null) first = null; else prev.next = null; size--; modCount++; return element; }
publicintindexOf(Object o){ int index = 0; if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) return index; index++; } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) return index; index++; } } return -1; }
publicbooleanremove(Object o){ if (o == null) { for (Node<E> x = first; x != null; x = x.next) { if (x.item == null) { unlink(x); returntrue; } } } else { for (Node<E> x = first; x != null; x = x.next) { if (o.equals(x.item)) { unlink(x); returntrue; } } } returnfalse; }
public E remove(int index){ checkElementIndex(index); return unlink(node(index)); }
publicvoidclear(){ // Clearing all of the links between nodes is "unnecessary", but: // - helps a generational GC if the discarded nodes inhabit // more than one generation // - is sure to free memory even if there is a reachable Iterator for (Node<E> x = first; x != null; ) { Node<E> next = x.next; x.item = null; x.next = null; x.prev = null; x = next; } first = last = null; size = 0; modCount++; }
从头到尾删除,最后回到最初first和last都指向空的状态。
get(int index)
得到指定索引出的元素。
1 2 3 4
public E get(int index){ checkElementIndex(index); return node(index).item; }
set(int index, E element)
设定指定索引处的值
1 2 3 4 5 6 7
public E set(int index, E element){ checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; }
push(E e) & pop()
链表头插入和删除。
1 2 3 4 5 6 7
publicvoidpush(E e){ addFirst(e); }
public E pop(){ return removeFirst(); }
clone()
复制一份一样的链表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
public Object clone(){ LinkedList<E> clone = superClone();
// Put clone into "virgin" state clone.first = clone.last = null; clone.size = 0; clone.modCount = 0;
// Initialize clone with our elements for (Node<E> x = first; x != null; x = x.next) clone.add(x.item);
return clone; }
toArray()
转化为数组。
1 2 3 4 5 6 7
public Object[] toArray() { Object[] result = new Object[size]; int i = 0; for (Node<E> x = first; x != null; x = x.next) result[i++] = x.item; return result; }