【问题标题】:Java ArrayQueue enqueue and dequeue throwing arrayQueueExceptionJava ArrayQueue入队和出队抛出arrayQueueException
【发布时间】:2015-06-19 15:34:07
【问题描述】:

我在尝试从 ArrayQueue> 变量类型出列到 ArrayQueue 并将其作为参数传递给另一个方法时遇到了一些问题。

所以基本上我有两种方法,一种是不带参数的 sort(),另一种是接受 2 个 ArrayQueue 参数的 merge()。

我有一个名为 Queue 的全局变量:

private ArrayQueue<ArrayQueue<E>> Queue;

在我的 sort() 中,我将前两项从 Queue 中出列,并将它们作为参数传递给 merge():

public void sort() {
    while (Queue.size() >= 2) {
        ArrayQueue<E> first = Q.dequeue();
        ArrayQueue<E> second = Q.dequeue();
        System.out.print("1st" + firstElement);
        System.out.print("2nd" + secondElement);
         merge(first,second)
    }
}

public E dequeue() throws ArrayQueueException {
    if (isEmpty() == true)
        throw new ArrayQueueException("Queue error");
    if (front == rear) {
        front = front - 1;
        rear = rear - 1;
    } else
        front = front + 1;
    size--;
    return Q[front];
}

然后在我的合并中:

private ArrayQueue<E> merge(ArrayQueue<E> q1, ArrayQueue<E> q2) throws ArrayQueueException {
    System.out.println("q1 is " + q1.toString());
    ArrayQueue<E> merged = new ArrayQueue<E>();
    String e1 = (String) q1.dequeue();
    System.out.println("e1 is " + e1);
    String e2 = (String) q2.dequeue();

    merged.enqueue(q2.dequeue());
            if (q2.isEmpty()) {
                // add remaining q1 elements
                while (!q1.isEmpty()) {
                    merged.enqueue(q1.dequeue());
                }
                break;
            }
            // take another element from q2
            e2 = (String) q2.dequeue();
}

我从控制台得到的输出是:

1st[the]2nd[ragged]q1 is [the]
e1 is null
Exception in thread "main" ArrayQueueException: Queue error
at ArrayQueue.dequeue(ArrayQueue.java:84)
at QueueSort.merge(QueueSort.java:41)
at QueueSort.sort(QueueSort.java:73)
at QueueSort.main(QueueSort.java:122)

QueueSort.java:41 是这一行:merged.enqueue(q2.dequeue());我不知道为什么它不会出队。

另外,我不确定为什么 e1 变为空,因为我试图执行出队并将其转换为字符串。我需要这个字符串用于后面的部分。 有任何想法吗?提前致谢。

【问题讨论】:

  • ArrayQueue 不是通用的 JDK 实用程序类。它实际上来自com.sun.jmx.remote.internal 包,因此没有记录,不应被客户端代码使用。这里正确的类可能是ArrayDeque(或LinkedList,随便你)
  • @Slanec 这实际上是一项任务,我们必须按照他们所说的去做。您对如何根据上述标准进行修复有任何想法吗?因为 merge() 的 ArrayQueue 参数每个只包含一个字符串,所以我在想是否可以将它从参数中取出,然后像上面的代码一样重新入队到另一个队列?我在想是不是因为那部分导致了问题?
  • @Slanec 嘿,有什么想法吗?

标签: java arrays queue


【解决方案1】:

主要问题似乎是您的出队功能。它总是返回 isEmpty()。我会检查 isempty() 并在你到达那个点之前检查大小。在您尝试出队之前,我没有看到任何项目被排队到 ArrayQueue 中。可能您设置了 q1 和 q2,但您没有在它们上面放置任何物品,因此它什么也没有返回。

一个可能的问题是队列的变量名。 Queue 是一个接口,在将它传递给出队之前,排序中的事情可能会变得混乱。您应该将 Queue 的名称更改为 queue 或更不同的名称。最好总是让变量以小写字母开头。

此外,对于样式,如果您从中返回布尔值,则在 dequeue 函数中,不需要将 isEmpty() 与 true 进行比较。它看起来像:

if (isEmpty())

isEmpty() 函数解析为真或假,if 语句作用于该值。

我希望这会有所帮助。

【讨论】:

  • 好吧,但我意识到现在的问题是假设我的 q1 包含 [the] 并且 q2 包含 [hello]。但是当我需要将这两个添加到另一个称为合并的 ArrayQueue 中时。将它们添加到合并中的唯一方法是入队。但为了使其入队,我需要 q1 和 q2 先出队。但是话又说回来,因为 q1 和 q2 只有 1 个项目,这就是为什么在出队时它会遇到 isEmpty
  • 应该q1和q2不能为空吧?当他们将单个项目出列时,它应该正确出列。您应该能够在您的队列中使用它。当其中只有一项时,您的 isEmpty() 函数可能会返回 true。
猜你喜欢
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多