第一回写的用arraylist模拟栈操作
2024-10-18 05:27:27
package hashMap;
import java.util.ArrayList;
import d.Student;
/**
* 用ArrayList模拟栈操作
* @author zhujiabin
* @see 2016年7月14日
*/ public class Stack
{
ArrayList<Student> al=new ArrayList<Student>();
public Object peek()
{
return al.get();
}
public Object pop()//出栈
{
return al.remove(al.size()-);
}
public void push(Student o)//进栈
{
al.add(o);
}
public void clear()//将栈置空
{
al.clear();
}
public boolean isEmpty()//判断栈是否是空
{
if(al.isEmpty())
{
return true;
}
else
{
return false;
}
}
public Object getIndex(int i)//返回指定下标出的值
{
return al.get(i);
}
}
测试:
package hashMap;
import d.Student;
/**
* 用ArrayList模拟栈操作
* @author 郑云飞
* @see 2010年8月14日
*/
public class StackTest
{
public static void main(String[] args)
{
Stack s1=new Stack();
s1.push(new Student("庄子",));
s1.push(new Student("老子",));
s1.push(new Student("梦子",));
s1.push(new Student("荀子",));
while(!s1.isEmpty())
{
System.out.println(s1.pop());//出栈输出内容
} }
} package hashMap;
/**
* 用ArrayList模拟栈操作
* @author zhujiabin
* @see 2016年7月14日
*/
public class Student
{
String name;
int age;
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return "姓名:" +name+"年龄:"+age;
}
}
最新文章
- vs2012相关配置
- HBase 安装过程记录
- 对象属性操作-包含kvc---ios
- URAL 1066 Garland 二分
- 通过maven添加quartz
- ubuntu安装hadoop2.6
- JQuery插件的学习
- UML(Unified Modeling Language)同一建模语言
- SQL进阶随笔--case用法(二)
- 你应该知道的 volatile 关键字
- python 试题
- Hadoop项目实战-用户行为分析之应用概述(三)
- C语言 &#183; 算年龄
- linux常用命令:df 命令
- [原][译][osg][osgEarth]飞行模拟软件JSBSim的操作(FGFCS类)
- 润乾报表JSF FORM 标签中使用填报表解决方案
- HDU 5154 Harry and Magical Computer bfs
- poj1228 Grandpa&#39;s Estate
- php 实现重定向的三种方式
- redis基础之redis-cluster(集群)(七)
热门文章
- Unity3D 之UGUI 切换开关(toggle)
- ASP.net程序在本地操作正确,新电脑不正确的处理经验
- O-C-11-利用类方法做一个简单的计算器
- asp.net:repeater嵌套(常用于新闻等在首页归类显示)
- caffe源码阅读(3)-Datalayer
- android软件开发之webView.addJavascriptInterface循环渐进【一】
- python 在调用时计算默认值
- variable-precision SWAR算法:计算Hamming Weight
- 身份证js验证
- REST接口规范