Resizing array笨办法,每次都要新建一个临时数组
来源:互联网 发布:淘宝开网店哪里进货 编辑:程序博客网 时间:2024/05/16 09:53
package princeton;
import java.util.NoSuchElementException;
public class MyResizingArrayQueue {
public String [] a;int count;public MyResizingArrayQueue(){ this.count = 0; this.a = new String [2];} public void resize(int capacity){ String [] temp = new String [capacity]; for(int i =0;i<count;i++){ temp[i] =a[i]; } a = temp;}public void enqueue(String s){ if(a.length==count){ resize(2*a.length); } a[count] = s; count++;}public String dequeue(){ if (isEmpty()) throw new NoSuchElementException("Stack underflow"); String s = a[0]; String [] temp = new String[count-1]; for (int i =0;i<count-1;i++){ temp[i] = a[i+1]; } for (int i =0;i<count-1;i++){ a[i] = temp[i]; } a[count-1] = null; //a = temp; count--; if(a.length==4*count){ resize(a.length/2); } return s;}public int sizeof(){ return count;}public boolean isEmpty(){ return count ==0;}public static void main(String[] args) { // TODO Auto-generated method stub MyResizingArrayQueue mraq = new MyResizingArrayQueue(); for(int i =0;i<args.length;i++){ mraq.enqueue(args[i]); //System.out.println(mraq.a[i]); //System.out.println(mraq.a.length); } for(int i =0;i<6;i++){ System.out.println(mraq.dequeue()); System.out.println("array length is "+mraq.a.length); }}
}
0 0
- Resizing array笨办法,每次都要新建一个临时数组
- VS2010每次新建工程都要属性设置之解决方法
- 发一个代码运行框。省得每次把网上的代码拷下来都要新建一个html页面,黏贴,保存运行。。。
- TortoiseGit 每次都要
- struts2为什么每次请求都要创建一个Action对象
- 关于大数据量的循环 每次都要操作数据库的办法
- 新建一个Object数组
- 安装office 2007 后,每次打开都要弹出一个 setup error 对话框
- 安装office 2007 后,每次打开都要弹出一个 setup error 对话框
- heat创建stack时为何要在keystone新建一个临时project
- 每次打开IE8都要设置
- office2007 每次打开都要配置
- 每次启动hadoop都要格式化namenode?
- unityvs每次都要输入license问题
- 每次开机都要扫描磁盘方法
- WORD2010每次启动都要配置
- office2007每次打开都要配置文件
- TortoiseGit 每次都要输入账号密码
- poj1599
- [LeetCode] Group Anagrams
- ubuntu 取色软件 Gpick
- POJ1988 Cube Stacking
- POJ-1258 Agri-Net(最小生成树[Prim])
- Resizing array笨办法,每次都要新建一个临时数组
- c实现极简单的正则表达式解析
- EXISTS的简单程序
- Android中的消息机制-源码分析
- Codeforces Gym100650C The Game of Efil
- 小P寻宝记——粗心的基友
- 专访罗升阳:老罗的Android之旅
- Interleaving String
- HTML5+ 打开关闭侧滑窗口