簡體   English   中英

在Java中使用數組創建循環隊列

[英]Creating a circular queue with arrays in Java

如果在隊列中輸入了三個以上的名稱,則會生成錯誤:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at hotelobjects.Queue.addqueue(Queue.java:17)
    at hotelobjects.HotelObjects.addCustomers(HotelObjects.java:82)
    at hotelobjects.HotelObjects.main(HotelObjects.java:44)
Java Result: 1

如何確保將在原始3后面輸入的任何名稱以循環方式放置在隊列的前面。

package hotelobjects;
import java.util.*;
import java.io.*;

public class HotelObjects {
static Queue myQueue = new Queue();
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        String command;

        Scanner input = new Scanner(System.in);

        Room[] myHotel = new Room[10];
        for (int x = 0; x < myHotel.length; x++) {
        myHotel[x] = new Room();
        }

        System.out.println("10 Rooms Created");

        while (true) {
            System.out.print("\nEnter command or 'X' to exit: ");
            command = input.next();
            command = command.toLowerCase();

            if (command.charAt(0) == 'x') {
                System.exit(0);
            }

            if (command.charAt(0) == 'a') {
                addCustomers(myHotel);
            }

            if (command.charAt(0) == '3') {
                displayNames(myHotel);
            }
        }
    }

    private static void addCustomers(Room myHotel[]) {
        String roomName;
        int roomNum;
        System.out.println("Enter room number (0-10) or 11 to exit:");
        Scanner input = new Scanner(System.in);
        roomNum = input.nextInt();
        if (roomNum<11) {
            System.out.println("Enter name for room " + roomNum + " :");
            roomName = input.next();
            roomName = roomName.toLowerCase();
            myHotel[roomNum].setName(roomName);
            myQueue.addqueue(roomName);
        }
        else {
            System.exit(0);
        }
    }

    private static void displayNames(Room[] myHotel) {
        System.out.println("The last 3 names entered are: ");
        for (int x = 0; x < 3; x++) {
            myQueue.takequeue();
        }
    } 
}

這是“隊列”類:

package hotelobjects;

public class Queue {
    String qitems[] = new String[3];
    int front = 0, end = 0;

    void addqueue(String roomName) {
        qitems[end] = roomName;
        end++;
    }

    void takequeue() {
        if (end > front) {
            System.out.println("Name Entered : " + qitems[front]);
            front++;
        } else {
            System.out.println("No Name");
        }
    }
}

增加索引模塊隊列中元素的最大數量:

void addqueue(String roomName) {
    qitems[end] = roomName;
    end = (end + 1) % 3;
}

這樣就得到:

end = (0 + 1) % 3 = 1
end = (1 + 1) % 3 = 2
end = (2 + 1) % 3 = 0
end = (0 + 1) % 3 = 1
...

您還應該更改takequeue()方法,以考慮隊列現在是循環的事實。 像這樣:

void takequeue() {
    System.out.println("Name Entered : " + qitems[front]);
    front = (front + 1) % 3;
}

創建一個常量來存儲隊列容量而不是在代碼中重復3值也是一個好主意:

private static final int CAPACITY = 3;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM