【问题标题】:Creating an Array of Queues创建队列数组
【发布时间】:2015-03-06 11:42:19
【问题描述】:

我必须用 Java 编写一个程序,其中包含从我创建的队列类创建的队列数组,如下所示 -

class Queue1{  //each line at the supermarket

    public Queue1(int s){                             //constructor
        maxSize = s;
        pplLines = new long[maxSize];
        front = 0;
        rear = -1;
        nItems = 0;
    }

    public void insert(long j){                    //put item at rear of queue
        if(rear == maxSize - 1)                    //deal with wraparound
            rear = -1;
        pplLines[++rear] = j;                      //increment rear and insert
        nItems++;                                  //one more item
    }

    public long remove(){                          //take item from front of queue
        long temp = pplLines[front++];             //get value and increment front
        if(front == maxSize)                       //deal with wraparound
            front = 0;                             
        nItems--;                                  //one less item
        return temp;
    }

    public long peekFront(){                       //peek at front of queue
        return pplLines[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(){                                         //display contents of queue
        for (int i = 0; i < nItems; i++) {
        System.out.println(pplLines[(front + i) % maxSize]);
    }
    }


        }//ends queue class

那么如何将队列类引用到以此开头的数组中

公共超市(int num_clerks, int line_size){

之后,我创建了队列数组,我需要找到最短的线路来插入客户,我必须在其中输入超市可用的店员/线路的数量,我将如何将其添加到我的 SuperMarket 类?

感谢您的帮助!

【问题讨论】:

  • 将字段Queue[] queues 添加到您的超市。此外,Java 已经有一个 Queue 类。

标签: java arrays class constructor queue


【解决方案1】:

如果你做类似的事情

public SuperMarket(int num_clerks, int line_size){ 

    List<Queue1> clerks = new ArrayList<Queue1>();
    for(int i; i< num_clerks; i++){
        clerks.add(new Queue1(line_size))
    }
}

【讨论】:

    猜你喜欢
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 2017-01-17
    • 1970-01-01
    • 2021-01-27
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多