我检查了Java.util.LinkedList类,发现链表类提供了几种方法
public void addFirst(E e)
public boolean offerFirst(E e)
public void push(E e)这3种方法都会在列表的头部添加一个元素。那么为什么不同的实现需要相同的函数呢?
是不是因为推送是为Stack和
offerFirst -出列
addFirst - LinkedList
或者其他一些基本原理?
请在这里分享一些见解。谢谢
发布于 2015-12-01 15:34:20
由于Deque接口的契约,这些都是在LinkedList中实现的。
Deque的javadocs清楚地解释了其中的区别:
空addFirst(E e)
如果可以在不违反容量限制的情况下立即执行此操作,则在此双端队列的前面插入指定的元素。当使用容量受限的双队列时,通常首选使用方法offerFirst(E)。
布尔offerFirst(E e)
将指定的元素插入到此双精度队列的前面,除非它违反了容量限制。在使用容量受限的双端队列时,此方法通常比addFirst(E)方法更可取,后者只能通过抛出异常才能无法插入元素。
ArrayBlockingQueue类(javadocs)是实现Deque的有界队列实现的一个示例。
发布于 2015-12-01 15:36:54
看一看java文档..
public void addFirst(E e)
// Inserts the specified element at the beginning of this list.
public boolean offerFirst(E e)
// Inserts the specified element at the front of this list.
public void push(E e)
// Pushes an element onto the stack represented by this list.有关更多信息,请访问:LinkedList
https://stackoverflow.com/questions/34014814
复制相似问题