数据结构中的栈在java中有封装好的类,可以直接调用
Stack:
empty | 判断stack是否为空 |
peek | 查看栈顶部的对象,但不从栈中移除它 |
pop | 移除栈顶部的对象,并作为此函数的值返回该对象 |
push | 将对象压入栈顶部 |
search | 返回最靠近栈顶的对象到顶端的距离 |
Stack stack = new Stack()创建一个空栈
int a = (Integer)stack.peek();int类型赋值
double a = (double)stack.peek();double类型赋值
char a = stack.peek().toString().charAt(0);char类型赋值
String a=(String)stack.peek();String类型赋值
String a=stack.peek().toString();String类型赋值
遍历方法
—–1—–
for (Integer x : s) { //不弹出
System.out.println(x);
}
—–2—–
while (!s.empty()) { //弹出
System.out.println(s.pop());
}