5

我在java中做一个队列。这是我的代码:

public class ListQueue {
public static void main(String[] args){
    Queue myQueue;
    Scanner sc = new Scanner(System.in);
    String input;
    int choice = 99;
    do{
        System.out.println("================");
        System.out.println("Queue Operations Menu");
        System.out.println("================");
        System.out.println("1,Enquene");
        System.out.println("2,Dequeue");
        System.out.println("3,Empty?");
        System.out.println("4,Count?");
        System.out.println("5,View Queue");
        System.out.println("0, Quit\n");
        System.out.println("Enter Choice:");
        try{
            choice = sc.nextInt();
            switch(choice){
            case 1:
                System.out.println("Please enter name: ");
                input = sc.next();
                myQueue.enqueue(input);
                System.out.println(input + "is successful queued");
                break;
            case 2:
                if(myQueue.isEmpty()){

                }
                break;
            case 3:
                if(myQueue.isEmpty()){
                    System.out.println("Queue is empty");
                }else{
                    System.out.println("Queue is not empty");
                }
                break;
            case 4:
                System.out.println("Number of people is " + "the queue" + myQueue.size());
                break;
            case 5:
                if(!myQueue.isEmpty())
                    myQueue.viewQueue();
                else
                    System.out.println("Queue is empty");
                break;
            case 0:
                System.out.println("Good-bye");
                break;
            default:
                    System.out.println("Invalid choice");
            }
        }
        catch(InputMismatchException e){
            System.out.println("Please enter 1-5, 0 to quit");
            sc.nextLine();
        }
    }while(choice != 0);
}

}

但是,我在 enqueue() 和 viewQueue() 有错误,我想知道为什么。我是否以错误的方式声明队列?提前致谢。我是新来排队的,所以请多多包涵。

4

4 回答 4

13

Java 队列没有入队和出队方法,这些操作是使用以下方法完成的:

排队:

  • add(e): 插入对象失败抛出异常
  • offer(e): 如果插入对象失败返回 false

出队:

  • remove(): 如果队列为空则抛出异常
  • poll(): 如果队列为空,则返回 null

看看队列中的第一个对象:

  • element(): 如果队列为空则抛出异常
  • peek(): 如果队列为空,则返回 null

Queue 从 Collection 继承的 add 方法插入一个元素,除非它违反队列的容量限制,在这种情况下它会抛出IllegalStateException. offer 方法仅用于有界队列,与 add only 的不同之处在于它通过返回 false 来指示插入元素失败。

(参见:http ://docs.oracle.com/javase/tutorial/collections/interfaces/queue.html )

您也可以检查它,因为这更有用:

http://docs.oracle.com/cd/B10500_01/appdev.920/a96587/apexampl.htm

于 2013-07-09T12:39:05.160 回答
3

你还没有初始化 myQueue:

Queue myQueue;

请注意,Queue 是一个抽象类,您需要将 myQueue 初始化为适当的实现。

参考下面的javaDoc:

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Queue.html

于 2013-07-09T12:31:23.203 回答
1

@顺便说一句,我如何循环遍历队列中的元素?你能给我举个例子吗?</p>

我刚刚在您的案例 5 中添加了功能:用于查看列表元素。

case 5:
    if(!myQueue.isEmpty())
    {
    for(Iterator it=myQueue.iterator();it.hasNext();)
        System.out.println(it.next());
//or using as a Object in Enhanced for loop
        //for (Object element : myQueue)
        //System.out.println(element);
    }
    else
        System.out.println("Queue is empty");
    break;

正如 Ryman Holmes 建议的那样,您可以使用 ArayDeque。

Queue<String> myQueue = new ArrayDeque<String>();

优点: - 它比 Stack 和 LinkedList 更快 - ArrayDeque 没有容量限制,因此它们可以根据需要增长以支持使用。

风险: - 它们不是线程安全的;在没有外部同步的情况下。- 它们不支持多个线程的并发访问。

于 2016-10-15T20:44:02.000 回答
0

就像zerocool说你没有初始化队列,因为队列是一个接口,你不能直接实例化一个接口,你需要实例化一个实现接口的类,Queue例如ArrayDequeueLinkedList等等......

要初始化队列并消除错误,您需要以下内容:

Queue<Integer> myQueue = new ArrayDeque<Integer>();

或者

Queue<String> myQueue = new LinkedList<String>();

这是您可以实例化的队列类型的 API

另见

于 2014-01-18T14:33:26.747 回答