Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅使用队列反转队列。获取indexofbound异常_Java_Queue - Fatal编程技术网

Java 仅使用队列反转队列。获取indexofbound异常

Java 仅使用队列反转队列。获取indexofbound异常,java,queue,Java,Queue,我的插入和删除方法的实现: public static Queue1 mirror(Queue1 s){ int len = s.getSize(); Queue1 ret = new Queue1(s.getSize()); Queue1 tmp = new Queue1(s.getSize()); while(ret.getSize()!=len){ while(s.getSize()>1) tmp.insert(s.remove()

我的插入和删除方法的实现:

public static Queue1 mirror(Queue1 s){
    int len = s.getSize();
    Queue1 ret = new Queue1(s.getSize());

    Queue1 tmp = new Queue1(s.getSize());

    while(ret.getSize()!=len){
        while(s.getSize()>1) tmp.insert(s.remove());
        ret.insert(s.remove());
        while(tmp.getSize()>1) s.insert(tmp.remove());
        ret.insert(tmp.remove());
    }

    return ret;
}
除非我将
Tmp
的大小增加到
s.getSize()+2
,并且在本例中,它打印一个零,否则代码不会工作


有人能解释一下发生了什么吗?

看起来您的循环缺少几个条件

你的问题是:

public void insert(int x){
    if(rear == maxsize-1) rear = -1;
    arr[++rear] =  x;

    count++;
}

public int remove(){
    int tmp = arr[front++];
    if(front==maxsize) front = 0;

    count--;
    return tmp;
}
假设您的输入是

while(ret.getSize()!=len){
    while(s.getSize()>1) tmp.insert(s.remove()); // 1
    ret.insert(s.remove()); // 2
    while(tmp.getSize()>1) s.insert(tmp.remove()); // 3
    ret.insert(tmp.remove()); // 4
}
在第一次执行1之后:

s = [1 2 3]
第2次之后:

s = [3]
tmp = [1 2]
ret = []
第3次之后:

s = []
tmp = [1 2]
ret = [3]
第4次之后:

s = [1]
tmp = [2]
ret = [3]
然后重复步骤1,但不执行任何操作:

s = [1]
tmp = []
ret = [3 2]
然后,第2步:

s = [1]
tmp []
ret = [3 2]
现在您已经完成了,但是在终止之前,您仍然需要在这里执行3和4。步骤3将不起任何作用,但步骤4将给您一个错误,因为您正试图从空队列
tmp
中删除一个项目

解决方案是添加两个条件:

s = []
tmp = []
ret = [3 2 1]

我希望有帮助。我想自己找到解决这个问题的办法:只使用队列反转队列。以下是我的想法:

while(ret.getSize()!=len){
    while(s.getSize()>1) tmp.insert(s.remove());
    if (s.getSize() > 0) ret.insert(s.remove());
    while(tmp.getSize()>1) s.insert(tmp.remove());
    if (tmp.getSize()>0) ret.insert(tmp.remove());
}
公共类队列1{
私人整数最大值,计数,前,后;
私有int[]arr;
公共队列1(int-maxSize){
arr=新整数[maxSize];
this.maxSize=maxSize;
计数=0;
正面=0;
后部=0;
}
公共布尔插入(int x){
如果(计数==maxSize)
返回false;
arr[后]=x;
后=(后+1)%maxSize;
计数++;
返回true;
}
public int remove()引发异常{
如果(计数=0){
抛出新异常(“无法从队列中删除:它为空!”);
}
int ret=arr[前];
前=(前+1)%maxSize;
计数--;
返回ret;
}
公共int getSize(){
返回计数;
}
公共静态Queue1镜像(Queue1 q)引发异常{
int n=q.getSize();
Queue1 ret=新的Queue1(n);
Queue1 tmp=新的Queue1(n);
布尔f=真;
while(ret.getSize()1)
tmp.插入(q.移除());
重新插入(q.移除());
}否则{
while(tmp.getSize()>1)
q、 插入(tmp.remove());
ret.insert(tmp.remove());
}
f=!f;
}
返回ret;
}
}

希望有帮助!这个解决方案有效。它遍历所有队列并到达最后一个元素,并将其保存在
ret
队列中。它对所有元素都执行此操作。

这些代码段中哪一个不起作用?什么能让它起作用?
public class Queue1 {
    private int maxSize, count, front, rear;
    private int[] arr;

    public Queue1(int maxSize) {
        arr = new int[maxSize];
        this.maxSize = maxSize;
        count = 0;
        front = 0;
        rear = 0;
    }

    public boolean insert(int x) {
        if (count == maxSize)
            return false;
        arr[rear] = x;
        rear = (rear + 1) % maxSize;
        count++;
        return true;
    }

    public int remove() throws Exception {
        if (count == 0) {
            throw new Exception("Cannot remove from queue: it's empty!");
        }
        int ret = arr[front];
        front = (front + 1) % maxSize;
        count--;
        return ret;
    }

    public int getSize() {
        return count;
    }

    public static Queue1 mirror(Queue1 q) throws Exception {
        int n = q.getSize();
        Queue1 ret = new Queue1(n);
        Queue1 tmp = new Queue1(n);
        boolean f = true;
        while (ret.getSize() < n) {
            if (f) {
                while (q.getSize() > 1)
                    tmp.insert(q.remove());
                ret.insert(q.remove());
            } else {
                while (tmp.getSize() > 1)
                    q.insert(tmp.remove());
                ret.insert(tmp.remove());
            }
            f = !f;
        }
        return ret;
    }
}