本篇继续上一部分,继续分析LinkedList的剩余一些方法
linkedList.offer("");//add(e)、addLast(e)
linkedList.offerFirst("");//addFirst(e)
linkedList.offerLast("");//addLast(e)
linkedList.peek();//peekFirst
linkedList.peekFirst();
linkedList.peekLast();
linkedList.poll();//pollFirst
linkedList.pollFirst();
linkedList.pollLast();
linkedList.pop();//removeFirst()
linkedList.push("");//addFirst(e)
linkedList.set(0,"");
1)offer(E e)方法,同add(E e)方法、addLast(E e)方法和offerLast(E e)方法:添加指定元素作为列表的尾部(最后一个元素)。时间复杂度是O(1)。
/**
* Adds the specified element as the tail (last element) of this list.
*
* @param e the element to add
* @return {@code true} (as specified by {@link Queue#offer})
* @since 1.5
*/
public boolean offer(E e) {
return add(e);
}
2)offerFirst(E e)方法,同addFirst(e)方法:将指定的元素插入到列表的开头,此时的时间复杂度是O(1)。
/**
* Inserts the specified element at the front of this list.
*
* @param e the element to insert
* @return {@code true} (as specified by {@link Deque#offerFirst})
* @since 1.6
*/
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
3)peek()方法同peekFirst()方法:检索但不删除此列表的头(第一个元素,可为null)。时间复杂度是O(1)。
/**
* Retrieves, but does not remove, the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
4)peekLast()方法:检索但不删除此列表的最后一个元素,如果此列表为空则返回{@code null}。时间复杂度是O(1)。
/**
* Retrieves, but does not remove, the last element of this list,
* or returns {@code null} if this list is empty.
*
* @return the last element of this list, or {@code null}
* if this list is empty
* @since 1.6
*/
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
5)poll()方法同pollFirst()方法:检索并删除此列表的头(第一个元素,可为null)。时间复杂度是O(1)。
/**
* Retrieves and removes the head (first element) of this list.
*
* @return the head of this list, or {@code null} if this list is empty
* @since 1.5
*/
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
6)pollLast()方法:检索并删除此列表的尾(最后一个元素,可为null)。时间复杂度是O(1)。
/**
* Retrieves and removes the last element of this list,
* or returns {@code null} if this list is empty.
*
* @return the last element of this list, or {@code null} if
* this list is empty
* @since 1.6
*/
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
7)pop()方法同removeFirst()方法:从此列表所表示的堆栈中弹出一个元素。换句话说,删除并返回该列表的第一个元素。时间复杂度是O(1)。
/**
* Pops an element from the stack represented by this list. In other
* words, removes and returns the first element of this list.
*
* <p>This method is equivalent to {@link #removeFirst()}.
*
* @return the element at the front of this list (which is the top
* of the stack represented by this list)
* @throws NoSuchElementException if this list is empty
* @since 1.6
*/
public E pop() {
return removeFirst();
}
8)push(E e)方法同addFirst(e)方法:将一个元素压入由此列表表示的堆栈。换句话说,将元素插入到列表的前面。时间复杂度是O(1)。
/**
* Pushes an element onto the stack represented by this list. In other
* words, inserts the element at the front of this list.
*
* <p>This method is equivalent to {@link #addFirst}.
*
* @param e the element to push
* @since 1.6
*/
public void push(E e) {
addFirst(e);
}
9)set(int index, E element)方法:用指定的元素替换此列表中指定位置的元素。因为要循环遍历查找指定元素索引处的节点,因此时间复杂度为O(n)。
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index);
E oldVal = x.item;
x.item = element;
return oldVal;
}
以上这些就将LinkedList的一些基本方法分析完了,下一篇将这些方法的做一个整体汇总整理,看看哪些方法的作用是相同的,以及他们在使用中的一个分类,因为LinkedList可作为栈、队列、双端队列使用。