【问题标题】:Queue output incorrect? queue.offer(), queue.poll() and queue.peek()队列输出不正确? queue.offer()、queue.poll() 和 queue.peek()
【发布时间】:2015-11-10 03:59:04
【问题描述】:

我应该为家庭作业制作一个程序,该程序接受用户输入并显示特定输出。我创建了一个数组队列,当我为其提供名称时,有时在显示队列时只会出现空值。我不想直接回答,但我想了解数组队列是如何工作的,所以如果有人能指出我正确的方向,我将不胜感激。

public static MyQueue displayMenu(MyQueue queue) {
   // list of choices (array of Strings)
     String[] array = { "Offer Person", "Poll Person", "Peek Person", "Display Queue", "Exit Program"};
     int choice = 0;
  // display loop   
     while (choice != array.length-1) {
        choice = JOptionPane.showOptionDialog(null, // put in center of screen
           "Press a Button", // message to user
           "Queue (line) of People", // title of window
           JOptionPane.YES_NO_CANCEL_OPTION, // type of option
           JOptionPane.QUESTION_MESSAGE, // type of message
           null, // icon
           array, // array of strings
           array[array.length - 1]); // default choice (last one)

        if(choice == 0) {
           String name = JOptionPane.showInputDialog(null, "Enter person's name");  
           queue.offer(name); 
        }
        if(choice == 1) {

           JOptionPane.showMessageDialog(null, queue.poll() + " is next in line.");  

        }
        if(choice == 2) {

           JOptionPane.showMessageDialog(null, queue.peek() + " is in front of the line.");


        }
        if(choice == 3) {
           String output = queue.toString();
           JOptionPane.showMessageDialog(null, output);
        }

     }
     return queue;
   }//close displayMenu
  }

这段代码是我class MyQueue<T> extends ArrayQueue<T> {的一部分

public String toString( ) {
  String s = "";
  for (int i = 0; i < currentSize; i++) {//loop through array and wrap around      
     endIndex = (endIndex + 1) % maxSize;
     s += "\n"+array[i];   
  } 
  return s; 
}

【问题讨论】:

    标签: java io queue


    【解决方案1】:

    ArrayQueue 类是您自己的实现吗?我怀疑这可能有问题,否则您的 displayMenu 代码看起来是正确的。这是您可以测试的SSCCE 类。我已经使用ArrayDeque 作为队列。

    import java.util.ArrayDeque;
    
    import javax.swing.*;
    
    public class QueueTest {
    
      public static MyQueue<String> displayMenu(MyQueue<String> queue) {
        // list of choices (array of Strings)
        String[] array = {"Offer Person", "Poll Person", "Peek Person", "Display Queue", "Exit Program"};
        int choice = 0;
        // display loop
        while (choice != array.length - 1) {
          choice = JOptionPane.showOptionDialog(null, // put in center of screen
                                                "Press a Button", // message to user
                                                "Queue (line) of People", // title of window
                                                JOptionPane.YES_NO_CANCEL_OPTION, // type of option
                                                JOptionPane.QUESTION_MESSAGE, // type of message
                                                null, // icon
                                                array, // array of strings
                                                array[array.length - 1]); // default choice (last one)
    
          switch(choice) {
            case 0:
              String name = JOptionPane.showInputDialog(null, "Enter person's name");
              queue.offer(name);
              break;
            case 1:
              JOptionPane.showMessageDialog(null, queue.poll() + " is next in line.");
              break;
            case 2:
              JOptionPane.showMessageDialog(null, queue.peek() + " is in front of the line.");
              break;
            case 3:
              String output = queue.toString();
              JOptionPane.showMessageDialog(null, output);
              break;
            case 4:
              System.out.println("Exiting program");
              break;
            default:
              System.out.println("Choice not valid");
              break;
          }
        }
        return queue;
      }//close displayMenu
    
      static class MyQueue<T>
        extends ArrayDeque<T> {
        public MyQueue() {
        }
      }
    
      public static void main(String[] args) {
        MyQueue<String> myQueue = new MyQueue<String>();
        displayMenu(myQueue);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多