java对栈的常用操作
栈Stack是Vector的子类,属于线性数组,主要操作是出入栈,遵循“后进先出”原则。
public class Stack
extends Vector
// 非继承方法:
boolean empty()
Tests if this stack is empty.
E peek()
Looks at the object at the top of this stack without removing it from the stack.
E pop()
Removes the object at the top of this stack and returns that object as the value of this function.
E push(E item)
Pushes an item onto the top of this stack.
int search(Object o)
Returns the 1-based position where an object is on this stack
—
package com.mldn;
import java.util.Stack;
public class StackDemo
{
public static void main(String[] args)
{
Stack
stack.push(“a”); // 入栈, 压栈
stack.push(“b”);
stack.push(“c”);
while (!stack.isEmpty())
{
System.out.print(stack.pop() + ” “); // 出栈,弹栈
}
System.out.println();
}
}
/*
ubuntu@xu-desktop:~$ java com.mldn.StackDemo
c b a
*/
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java对栈的常用操作》
本文地址:http://www.xiupu.net/archives-126.html
关注公众号:
微信赞赏
支付宝赞赏