如何使这个Java queue display()方法能够处理所有数组大小,而不仅仅是[5]? //Queue.java //演示队列 //要运行此程序:C>java QueueApp 类队列 { 私有int-maxSize; 专用长数组; 私人内部阵线; 私家车; 私人机构; 公共队列(int s)//构造函数 { maxSize=s; queArray=新长[maxSize]; 正面=0; 后部=-1; nItems=0; } 公共空间插入(长j) { 如果(后==最大尺寸-1) 后部=-1; 数组[++后]=j; nItems++; } 公共长距离移动() { 长temp=queArray[front++]; 如果(前==maxSize) 正面=0; 硝酸盐--; 返回温度; } 公共长廊( { 返回数组[前]; } 公共布尔值isEmpty()//如果队列为空,则为true { 返回值(nItems==0); } public boolean isFull()//如果队列已满,则为true { 返回值(nItems==maxSize); } public int size()//队列中的项目数 { 返回nItems; } 公共空间显示() {int startFront=前面; 对于(int j=front;j
队列的后备存储是:如何使这个Java queue display()方法能够处理所有数组大小,而不仅仅是[5]? //Queue.java //演示队列 //要运行此程序:C>java QueueApp 类队列 { 私有int-maxSize; 专用长数组; 私人内部阵线; 私家车; 私人机构; 公共队列(int s)//构造函数 { maxSize=s; queArray=新长[maxSize]; 正面=0; 后部=-1; nItems=0; } 公共空间插入(长j) { 如果(后==最大尺寸-1) 后部=-1; 数组[++后]=j; nItems++; } 公共长距离移动() { 长temp=queArray[front++]; 如果(前==maxSize) 正面=0; 硝酸盐--; 返回温度; } 公共长廊( { 返回数组[前]; } 公共布尔值isEmpty()//如果队列为空,则为true { 返回值(nItems==0); } public boolean isFull()//如果队列已满,则为true { 返回值(nItems==maxSize); } public int size()//队列中的项目数 { 返回nItems; } 公共空间显示() {int startFront=前面; 对于(int j=front;j,java,methods,queue,Java,Methods,Queue,队列的后备存储是: // Queue.java // demonstrates queue // to run this program: C>java QueueApp class Queue { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; public Queue(int s) // const
// Queue.java
// demonstrates queue
// to run this program: C>java QueueApp
class Queue
{
private int maxSize;
private long[] queArray;
private int front;
private int rear;
private int nItems;
public Queue(int s) // constructor
{
maxSize = s;
queArray = new long[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
public void insert(long j)
{
if(rear == maxSize-1)
rear = -1;
queArray[++rear] = j;
nItems++;
}
public long remove()
{
long temp = queArray[front++];
if(front == maxSize)
front = 0;
nItems--;
return temp;
}
public long peekFront()
{
return queArray[front];
}
public boolean isEmpty() // true if queue is empty
{
return (nItems==0);
}
public boolean isFull() // true if queue is full
{
return (nItems==maxSize);
}
public int size() // number of items in queue
{
return nItems;
}
public void display()
{ int startFront = front;
for (int j = front ;j <nItems; j++ )
{
System.out.println(queArray[j]);
if (j == nItems-1 )
{ j=0;
System.out.println(queArray[j]);
}
if (j==startFront-1)
return;
}
}
}
class QueueApp
{
public static void main(String[] args)
{
Queue theQueue = new Queue(5); // queue holds 5 items
theQueue.insert(10); // insert 4 items
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove(); // remove 3 items
theQueue.remove(); // (10, 20, 30)
theQueue.remove();
theQueue.insert(50); // insert 4 more items
theQueue.insert(60); // (wraps around)
theQueue.insert(70);
theQueue.insert(80);
theQueue.display();
while( !theQueue.isEmpty() ) // remove and display
{ // all items
long n = theQueue.remove(); // (40, 50, 60, 70, 80)
System.out.print(n);
System.out.print(" ");
}
System.out.println("");
} // end main()
} // end class QueueApp
为什么不改为使用:
private long[] queArray;
在构造函数中。一旦您真正理解了该代码,您就可以自己开始担心重新调整逻辑大小了。只需使用以下方法:
queArray = new ArrayList<Long>();
public void display(){
对于(int i=0;i
现在发生的事情是,j
是元素在数组中的位置,它与到目前为止打印的元素数量不同
您需要使用不同的索引来计算打印的元素数,或者通过将j
与rear
进行比较来检查是否在末尾。当队列已满(rear==maxSize-1)并且您进行插入时,它将替换第一个
项,因此我认为当队列已满时,不应增加nItems++行
编辑:当您不需要模运算时,请避免模运算,它们会消耗大量cpu。在我看来,您使用了太多不需要的变量。您只需要队列大小及其项目计数
public void display() {
for (int i = 0; i < nItems; i++) {
System.out.println(queArray[(front + i) % maxSize]);
}
}
公共队列(int-s){
尺寸=s;
queArray=新长[s];
nItems=0;
}
公共空间插入(长j){
如果(nItems<尺寸){
queArray[nItems]=j;
nItems++;
}
}
公共长距离移动(){
如果(nItems>0){
长温度=数组[nItems];
硝酸盐--;
返回温度;
}
}
公共空间显示(){
对于(int j=0;j
Queue theQueue=新队列(5);不要将队列大小硬编码为5,由输入决定大小。使用Scanner询问队列大小。根本不解决问题。这是为了分配任务,可能必须这样做。@事实上,我不同意。我要指出他当前的实现不起作用的原因,以及他可以做些什么来修复它。@Dan,我不想猜测他会做什么是且不允许在其任务中使用。@阿米拉法尼当前的实现(几乎)工作正常,而且数组的可调整大小与此无关:它看起来像是一个具有上限容量的队列的教科书式循环数组实现。将其替换为列表将不会改变任何东西。这非常有效,但我只有一个问题。我理解For循环的工作原理,它将为队列中的每个项执行下面的操作e数组。现在我可以读取,打印到屏幕上,front的值加上i,%在代码中到底做了什么?这是我第一次看到它被使用。%
是模运算符,也称为余数。假设i
无限递增,i%5
的值将是0、1、2、3、4、0、1、2、3、4、0、1、2等等。
public void display() {
for (int i = 0; i < nItems; i++) {
System.out.println(queArray[(front + i) % maxSize]);
}
}
public Queue(int s) {
size = s;
queArray = new long[s];
nItems = 0;
}
public void insert(long j) {
if(nItems < size) {
queArray[nItems] = j;
nItems++;
}
}
public long remove() {
if(nItems > 0) {
long temp = queArray[nItems];
nItems--;
return temp;
}
}
public void display() {
for(int j = 0; j < nItems; j++) {
System.out.println(queArray[j]);
}
}