想要描写关于队列先进先出的算法
package com.stack;
public class Tested {
public int[] arr1;
public int size;
private final static int Cap = 10;
//无参构造(初始化数组)
public Tested(){
size = 0;
arr1 = new int[Cap];
}
//判零
public boolean isEmpty(){
if(size == 0){
return true;
}
return false;
}
//重定义数组长度
private void resize(int newlen){
int[] newArr = new int[newlen];
for(int i = 0;i < size;i++){
newArr[i] = arr1[i];
}
arr1 = newArr;
}
//插入元素
public void push(int count){
if(size == arr1.length)
{
resize(arr1.length * 2);
}
arr1[size] = count;
size++;
}
//弹出元素
public int pop(){
int[] arr2 = new int[arr1.length-1];
if(isEmpty()){
return -1;
}
int ret = arr1[size-1];
int i = 0;
while(size!=1){
arr2[i] = arr1[size - 1];
size--;
i++;
}
int size = 0;
while(i!=0){
arr1[size] = arr2[i-1];
size++;
i--;
}
size--;
return ret;
}
//输出函数
public void print(){
String str = "[";
if(arr1 == null){
str+="]";
}else {
for (int i = 0; i < size; i++) {
str += arr1[i];
if (i != size - 1) {
str += ", ";
} else {
str += "]";
}
}
}
System.out.println(size);
System.out.println("a.length="+arr1.length);
System.out.println(str);
}
public static void main(String[] args) {
Tested t1 = new Tested();
for (int i =0;i < 10;i++) {
t1.push(i);
t1.print();
}
for (int i =0;i < 10;i++) {
t1.pop();
t1.print();
}
}
}
在执行完t1.pop后,我想输出改变后的队列,结果队列中size值在执行prin函数时突然不知什么原因变成1了,导致一直只输出一个值。