jdk1.6以后的新特性:
addFirst(); offerFirst();
addLast(); offerLast();
getFirst(); peekFirst();
getLast(); peekLast();
removeFirst(); pollFirst();
removeLast(); pollLast();
jdk1.6以后,删除或获取元素时,如果列表为空,返回的是null,而不是抛异常;插入元素时返回的是boolean值
模拟堆栈和队列:
堆栈:先进后出;队列是先进先出
- public class DuiZhan {
- /**
- * 用linkedlist模拟堆栈,先进后出;队列,先进先出
- */
- private LinkedList<Object> l;
- DuiZhan(){
- l=new LinkedList<Object>();
- }
- public void myAdd(Object obj){
- l.addLast(obj);//队列:l.addFirst(obj);
- }
- public Object myGet(){
- return l.removeLast();
- }
- public boolean ifEmpty(){
- return l.isEmpty();
- }
- }
复制代码 |
|