【问题标题】:Enqueue, Dequeue and ViewQueue in JavaJava中的入队、出队和视图队列
【发布时间】:2013-07-07 01:19:33
【问题描述】:

我在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() 有错误,我想知道为什么。我是否以错误的方式声明队列?提前致谢。我是新来排队的,所以请多多包涵。

【问题讨论】:

    标签: java queue


    【解决方案1】:

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

    排队:

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

    出队:

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

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

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

    Queue 继承自 Collection 的 add 方法,插入一个 元素,除非它会违反队列的容量限制,在 在这种情况下它会抛出IllegalStateException。报价方法,即 仅用于有界队列,不同于 add only in 表示插入元素失败,返回false。

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

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

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

    【讨论】:

    • 顺便问一下,我如何循环遍历队列中的元素?你能给我发一些例子吗
    【解决方案2】:

    你还没有初始化myQueue:

    Queue myQueue;
    

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

    参考下面的javaDoc:

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

    【讨论】:

      【解决方案3】:

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

      我刚刚在您的案例 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 没有容量限制,因此它们可以根据需要增长以支持使用。

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

      【讨论】:

        【解决方案4】:

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

        要初始化队列并消除错误,您需要这样的东西:

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

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

        Here's the API for the type of Queues you can instantiate

        Also see

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-07
          • 1970-01-01
          • 2012-11-05
          • 2016-12-13
          相关资源
          最近更新 更多