A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 曹晓涵 中级黑马   /  2012-3-11 17:37  /  2357 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

如何用Java代码实现一个栈操作,比如将一个班的学生学号信息压入栈中,然后再将其取出?

评分

参与人数 1技术分 +1 收起 理由
老罗 + 1

查看全部评分

1 个回复

倒序浏览
本帖最后由 李明伟 于 2012-3-11 18:07 编辑

public class UsingStack {
        public static void main(String[] args) {


                Student  stu1=new Student();
                Student  stu2=new Student();
                 Student  stu3=new Student();
                //构造栈对象,使用类型限制,只能存储Student数据
                Stack<Student> s = new Stack<Student>();
               
                //1、2、3依次入栈
                s.push(stu1);
                s.push(stu2);
                s.push(stu3);
               
                //3、2、1依次出栈
                System.out.println(s.pop());
                System.out.println(s.pop());
                System.out.println(s.pop());
        }
}

评分

参与人数 1技术分 +1 收起 理由
房宝彬 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马