java.util.Stack

Stack(栈)继承了Vector类,底层实现是数组。

具有线程安全性,因为Vetor的增删查改方法都被线程同步了。

//将元素压栈,并返回该元素
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 sysnchronized E peek(){
 int len = size();
 if(len = 0)
   throw new EmptyStackException();
  return elementAt(len -1);
}

//判断栈是否为空
public boolean empty(){
return size()==0;
}

猜你喜欢

转载自blog.csdn.net/Anenan/article/details/83899695