首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >队列输出不正确?queue.offer()、queue.poll()和queue.peek()

队列输出不正确?queue.offer()、queue.poll()和queue.peek()
EN

Stack Overflow用户
提问于 2015-11-09 11:07:47
回答 1查看 142关注 0票数 0

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

代码语言:javascript
复制
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> {的一部分

代码语言:javascript
复制
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; 
}
EN

回答 1

Stack Overflow用户

发布于 2015-11-10 03:59:05

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

代码语言:javascript
复制
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);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33601918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档