概念
- 基本思想就是:先进后出
一、栈的创建
Stack<Integer> stack = new Stack<>();
二、栈的常用方法及作用
- 栈的父类Vector
public
class Stack<E> extends Vector<E> {
- 属于栈自己的方法
Stack<Integer> stack = new Stack<>();
/**
* 往栈顶添加一个元素,成功返回该元素
*/
stack.push(1);
stack.push(2);
stack.push(3);
/**
*移除栈顶元素,成功返回该元素,
* 如果栈为空,抛出异常
*/
stack.pop();
/**
* 查找栈顶元素,成功返回该元素
* 如果栈为空,抛出异常
*/
stack.peek();
/**
*如果栈为空,返回true,不为空,返回false
*/
stack.empty();
/**
*返回该元素距离栈顶的距离,该元素在栈顶返回1
* 如果不存在该元素,返回-1
*/
stack.search(3);
- 继承的父类的方法详见Vector()类中的方法