【问题标题】:How to make an queue array that can hold both strings and integers?如何制作一个既可以容纳字符串又可以容纳整数的队列数组?
【发布时间】:2020-11-18 20:35:45
【问题描述】:

我必须创建一个数组来保存用户输入,该输入是 3 个字母代码后跟一个票号。例如)Ama-34。我该怎么做呢? 我知道 long 是不正确的,我只是在另一个项目中对其进行建模。 我还必须允许我遇到困难的用户输入和操作。 这就是我目前所拥有的......


class QueueOrder{
    
    //Global Variables
    static Scanner orderScan = new Scanner(System.in);
    
    //Variables
    public int MaxSize;
    //How to make an array hold both names and numbers??
    public long[] BodaciousArray; 
    public int Front;  //Track the front pointer
    public int Rear;   //track the last pointer 
    public int NumberOfOrders; //track the number of orders in the system
    
    //Constructor
    public QueueOrder(int size){
        
        MaxSize = size;
        BodaciousArray = new long[MaxSize];
        Front = 0;
        Rear = -1;
        NumberOfOrders = 0;
    }
    
    //Enqueue - add to the rear of the queue
    //Allow the server to add one to the array
    public void Enqueue(){
        long j = 0;
        //Add a wrap around
        if(Rear == MaxSize - 1){           
            Rear = -1;
        }
        //Increment the rear and insert a new item
        BodaciousArray[++Rear] = j;
        NumberOfOrders++;
    }
   
    //Dequeue - remove one from the array
    //Allow the server to remove what is next in line
    public long Dequeue(){
        //Get the first value and incrament the front
        long temp = BodaciousArray[Front++];
        //Add a wrap around
        if(Front == MaxSize){
            Front = 0;
        }
        //Remove one item
        NumberOfOrders--;
        return temp;                
    }
    
    //Peek at the front of the queue
    //Allow the server to see what order is next
    public long peekFront(){
        return BodaciousArray[Front];
    }
    
    //Check to is the queue is empty
    public boolean isEmpty(){
        return(NumberOfOrders == 0);
    }
    
    //Check to see if the queue is full
    public boolean isFull(){
        return(NumberOfOrders == MaxSize);
    }
    
    //Check how many items are in the queue
    public int size(){
        return NumberOfOrders;
    }
    
    public void DisplayQueueOrder(){
        int i;
        if(Front == Rear){
            System.out.println("There are no orders to fill");
        }else{
            for(i = Front; i < Rear; i++){
                System.out.print("The current orders are: " 
                        + BodaciousArray[i] + ", ");
            }
        }
    }

【问题讨论】:

    标签: java arrays queue user-input


    【解决方案1】:

    如果你想保持两者分开,并且3个字母的代码是唯一的HashMap是要走的路:

    HashMap<String, Integer> queueArray = new HashMap<String, Integer>();
    queueArray.put("Ama", 34);
    System.out.println(arr.get("Ama"));
    

    输出:

    34
    

    否则,为什么不这样做:

    String[] tickets = {"Ama-34", "Abc-60", "Xyz-76"};
    
    public String getTicketCode(int index) {
        return tickets[index].split("-")[0];
    }
    
    public int getTicketNumber(int index) {
        return Integer.parseInt(tickets[index].split("-")[1]);
    }
    

    类似:

    System.out.println(getTicketCode(0));
    System.out.println(getTicketNumber(0));
    

    打印:

    Ama
    76
    

    【讨论】:

    • 好的,太棒了。谢谢!我将其设置为字符串而不是长字符串。我确实需要它们像 (Ama - 42) 一样打印在一起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2015-02-25
    • 2020-11-11
    相关资源
    最近更新 更多