第一次写的用arraylist模拟栈操作

package hashMap;
import java.util.ArrayList;
import d.Student;
/**
 * 用ArrayList模拟栈操作
 * @author 郑云飞
 * @see 2010年8月14日
 */

public class Stack
{
	ArrayList<Student> al=new ArrayList<Student>();
	public Object peek()
	{
		return al.get(0);
	}
	public Object pop()//出栈
	{
		return al.remove(al.size()-1);
	}
	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("庄子",200));
		s1.push(new Student("老子",200));
		s1.push(new Student("梦子",200));
		s1.push(new Student("荀子",200));
		while(!s1.isEmpty())
		{
			System.out.println(s1.pop());//出栈输出内容
		}
		
	}
}

 

package hashMap;
/**
 * 用ArrayList模拟栈操作
 * @author 郑云飞
 * @see 2010年8月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;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值