【问题标题】:Creating a circular queue with arrays in Java在 Java 中创建带有数组的循环队列
【发布时间】:2015-03-29 20:53:43
【问题描述】:

如果输入的名字超过 3 个,队列会产生错误:

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");
        }
    }
}

【问题讨论】:

    标签: java arrays queue


    【解决方案1】:

    增加你的索引模块队列中元素的最大数量:

    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;
    

    【讨论】:

    • 我尝试添加您的更改,但队列现在删除了原始名称
    • 循环队列就是这样工作的。如果需要在队列中存储更多元素,则需要增加其容量。
    • 问题出在takequeue()。我更新了答案,希望对您有所帮助。
    猜你喜欢
    • 2022-11-22
    • 2012-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多