stack是栈,特性是先进后出(filo,first in last out)。stack是继承于vector(矢量队列),由于vector是同数组实现的,stack也是通过数组而非链表。
stack和collection关系如下:
基于java8的源代码:
public class stack<e> extends vector<e> {
public stack() {//创建空栈
}
public e push(e item) {//入栈
addelement(item);
return item;
}
//出栈
public synchronized e pop() {
e obj;
int len = size();
obj = peek();
removeelementat(len - 1);
return obj;
}
//返回栈顶元素,但并不出栈
public synchronized e peek() {
int len = size();
if (len == 0)
throw new emptystackexception();
return elementat(len - 1);
}
//判断栈是否为空
public boolean empty() {
return size() == 0;
}
//查找元素并返回栈深
public synchronized int search(object o) {
int i = lastindexof(o);
if (i >= 0) {
return size() - i;
}
return -1;
}
//序列版本号
private static final long serialversionuid = 1224463164541339165l;
}
以上就是java集合之stack的图文代码分析的详细内容。