java对栈的常用操作

栈Stack是Vector的子类,属于线性数组,主要操作是出入栈,遵循“后进先出”原则。

public class Stack
extends Vector;// 是Vector的子类,属于List!线型的
// 非继承方法:
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 .util.Stack;
public class StackDemo
{
        public static void main(String[] args)
        {
                Stack stack = new Stack();        // 实例化栈
                stack.push(“a”);        // 入栈, 压栈
                stack.push(“b”);
                stack.push(“c”);
                while (!stack.isEmpty())
                {
                        System.out.print(stack.pop() + ” “);        // 出栈,弹栈
                }
                System.out.println();
        }
}
/*
ubuntu@xu-desktop:~$ com.mldn.StackDemo
c b a
*/

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《java对栈的常用操作
本文地址:https://www.zhiletu.com/archives-126.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微