繁体   English   中英

循环数组队列中的toString方法

[英]toString method in circular array queue

我需要创建一个循环数组队列,该队列允许用户“包装”该数组。 入队和出队方法已由我的老师核对,但我无法使队列正确打印。

public class MyArrayQueue<E> implements QueueInterface<E> {

    public static final int CAPACITY = 10;  
    private int capacity; 
    private E q[]; //array of E 
    private int f; //front 
    private int r; //rear

    //constructor
    public MyArrayQueue() {
         this (CAPACITY);
    }

    //constructor
    public MyArrayQueue(int n) {
        capacity = n;
        q = (E[]) new Object[capacity];
    }

    public void enqueue(E obj) throws FullQueueException {
        if(size() == capacity - 1) { //cannot hold more than n-1
            throw new FullQueueException("Full queue exception.");
    }

        q[r] = obj; //insert object in end of the queue
        r = (r + 1) % capacity; //wrap around r

    }

    public E dequeue() throws EmptyQueueException {
        if (isEmpty())
            throw new EmptyQueueException("Empty queue exception.");

        E temp = q[f]; //retrieve the front object
        q[f] = null; //good programming practice

        f = (f + 1) % capacity; //wrap around f

        return temp;
  }

  public E front() throws EmptyQueueException {
      if (isEmpty()) 
          throw new EmptyQueueException("Empty queue exception.");

      return q[f]; //return the front object without removing it
  }

  public boolean isEmpty() {
      return (f == r);
  }

  public int size() {
      return (capacity - f + r) % capacity;
  }

  //fix this loop for homework assignment, make it wrap around
  public String toString() {
      if (isEmpty())
          return "[]";

      String result = "[";

      result += q[f];

      for (int i = (f+1) % capacity; i != r; i = (i+1) % capacity) {
          result += " " +q[i];
      }
      return result + "]";
  }

} //end class

这也是我的客户课程。 我将toString方法更改为用户Jyr建议的方法,但仍然无法正确打印出来。 在客户端类中实现它可能会出错。

public static void main(String[] args) {

    Scanner console = new Scanner(System.in);

    MyArrayQueue<String> list = new MyArrayQueue<>();

    int capacity;
    int CAPACITY = 10;
    String q[];

    for (int i = 0; i < CAPACITY; i++) {
        capacity = i;
        q = new String[i];
    }

    String name;

    boolean flag = true;
    int num;

    while (flag) {
        System.out.println();
        System.out.println("1 ----- enqueue");
        System.out.println("2 ----- dequeue");
        System.out.println("3 ----- front");
        System.out.println("4 ----- ouput elements in the queue");
        System.out.println("5 ----- isEmpty");
        System.out.println("6 ----- size");
        System.out.println("0 ----- exit");
        System.out.println();

        System.out.println("Enter a command: ");
        num = console.nextInt();
        System.out.println();

        switch(num) {
            case 1:
                try {
                    System.out.println("Enter a word to enqueue: ");
                    name = console.next();
                    list.enqueue(name); 
                }
                catch (FullQueueException e) {
                    System.out.println("Full Queue");
                }
                break;
            case 2:
                try  {
                    System.out.println(list.dequeue());
                }
                catch (EmptyQueueException e){
                    System.out.println("Empty Queue");
                }
                break;
            case 3:
                try {
                    System.out.println(list.front());
                }
                catch (EmptyQueueException e) {
                    System.out.println("Empty Queue");
                }
                break;
            case 4:
                System.out.println(list.toString());
                break;
            case 5:
                System.out.println(list.isEmpty());
                break;
            case 6:
                System.out.println(list.size());
                break;
            case 0:
                flag = false;
                System.out.println("Thank you for using this program. ");
                break;
            default:
                System.out.println("Invalid input.");
                break;
        }
    }

}

我相信您使事情复杂化了。 您想要的是从f开始(例如i = f ),然后再向上移动一个,但是由于可以折回,因此我们应该向上移动(i + 1) % capacity 我们一直这样做直到到达终点(例如r )。 在进入此循环之前,我们应该验证队列不为空(以防止错误)。 如果实际上是空的,我们只返回带有两个方括号的String 因此您的代码看起来像这样;

public String toString() {

    if (isEmpty()) return "[]";
    String result = "[";

    for (int i = f; i != r; i = (i+1)%capacity) {
        result += q[i] + ", ";
    }   

    return result.substring(0,result.length()-2) + "]";
    // a bit dirty but gets the job done
}

或采用其他方法(关于方括号和逗号的打印);

public String toString() {

    if (isEmpty()) return "[]";
    String result = "[";

    result += q[f];

    for (int i = (f+1)%capacity; i != r; i = (i+1)%capacity) {
        result += ", " + q[i];
    }   

    return result + "]";

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM