数组模拟栈
来源:互联网 发布:哈希算法 编辑:程序博客网 时间:2024/06/01 08:52
import java.lang.ref.SoftReference;/** * 使用数组模拟Java Stack类 * 1.压入一个元素void push() * 2.查看一个元素E peek() * 3.弹出一个元素E pop() * 4.获得元素个数int size() * 5.清空栈void clear() * **/class StackDemo<E>{private int SIZE;private int LENGTH;private Object[] o;public StackDemo(){this.SIZE=0;this.LENGTH=1;o=new Object[1];}public void push(E e){this.o[this.SIZE++]=e;if(this.SIZE==this.LENGTH)this.copy();}public E pop(){if(this.SIZE==0)throw new ArrayIndexOutOfBoundsException();E temp=(E)this.o[--this.SIZE];this.o[this.SIZE]=null;return temp;}public E peek(){if(this.SIZE==0)throw new ArrayIndexOutOfBoundsException();return (E)this.o[this.SIZE-1];}public int size(){return this.SIZE;}public void clear(){this.SIZE=0;this.LENGTH=1;this.o=new Object[1];}private void copy(){Object[] temp=new Object[this.LENGTH*2];SoftReference<Object[]> sr=new SoftReference<Object[]>(temp);for(int i=0;i<this.SIZE;i++)temp[i]=this.o[i];this.o=temp;temp=null;this.LENGTH*=2;}public String toString(){StringBuilder sb=new StringBuilder();SoftReference<StringBuilder> sr=new SoftReference<StringBuilder>(sb);sb.append("[");for(int i=0;i<this.SIZE-1;i++)sb.append(this.o[i]+",");if(this.SIZE!=0)sb.append(this.o[this.SIZE-1]);sb.append("]");String str=sb.toString();sb=null;return str;}}//测试用码public class StackTest {public static void main(String[] args){StackDemo<String> sk=new StackDemo<String>();System.out.println(sk);sk.push("kkkk");sk.push("llll");sk.push("ssss");sk.push("oooo");System.out.println(sk);System.out.println(sk.pop()+"::"+sk.pop()+"::"+sk.peek());sk.clear();System.out.println(sk);sk.push("pppp");System.out.println(sk);}}
0 0
- 栈-数组模拟栈
- 用数组模拟栈
- 数组模拟栈
- 栈(数组模拟)
- Java 数组模拟栈
- 一个数组模拟两个栈
- 用数组去模拟栈
- 数组模拟队列和栈
- java用数组模拟栈
- 15.使用数组模拟栈
- 数组模拟栈+自定义异常
- 用数组模拟栈操作
- 用数组模拟栈操作
- 数据结构-用数组模拟栈
- 【栈】数组模拟栈的操作
- 数据结构03.1->Stack栈的模拟(数组)
- hdu 3887 树状数组(模拟栈)
- 3.1-一个数组模拟三个栈
- Delegate使用方法
- [C# 网络编程系列]专题六:UDP编程
- CodeWarrior烧录BSP过程详解
- Determine the Shape - UVa 11800 凸包判断四边形
- ASP.NET中JSON的序列化和反序列化
- 数组模拟栈
- ToolBox系列工具之Detector
- Android 学习笔记 Contacts (一)ContentResolver query 参数详解
- 黑马程序员-Java学习05-异常
- eclipse安装spring工具在线地址
- ODBC访问 Access数据库
- poj3322 Bloxorz I (bfs)
- ssh-keygen秘钥登陆(免密码)
- 使用iconv命令轻松实现linux下字符集编码的转换