循环队列java编程

循环队列java编程,java,Java,我正在尝试在队列中循环插入项目。。我有下面的代码,但没有正确插入。有什么问题吗 public boolean insert(int element){ if ( head == -1 && tail == -1 ){ head = 0; tail = 0; elements[tail] = element; return true; } else if((tail+1)%capacity

我正在尝试在队列中循环插入项目。。我有下面的代码,但没有正确插入。有什么问题吗

public boolean insert(int element){
    if ( head == -1 && tail == -1 ){
        head = 0;
        tail = 0;
        elements[tail] = element;
        return true;
    }  
    else if((tail+1)%capacity == head) {
        System.out.println("Full");
        return false;
    }
    else {
        tail = (tail+1)%capacity;
        elements[tail] = element;
        return true;
    }
}


代码对我来说运行良好。你能更准确地说明你的问题吗?问题何时发生?预期产量是多少?实际输出是什么?例如。我想这样插入一些元素:for(inti=0;i<6;i++){myQueue.insert(i+1);}。第一个元素总是空的。你能给出一个完整的例子(包括
main
method)吗?我怀疑第一个元素是
null
,因为您存储了原语。我用6个元素组成一个队列。正确的?它打印[null,1,2,3,4,5],右边是[1,2,3,4,5,6]。。现在更具体了?不太清楚。我无法重现这个错误。我使用了您的
insert
方法,并用
-1
初始化了
头部
尾部
。对我来说很好。
public boolean insert(int element){
    if (getCapacity() == capacity) {
        System.out.println("Queue is full.");
        return false;
    }else {
        elements[tail] = element;
        tail = (tail+1)%capacity;
        n++;
       return true;
    }
}
public int getCapacity(){
    return n;
}