如何迭代我自己实现的队列?

时间:2017-04-17 18:38:19

标签: java

我正在尝试调试程序的一部分,该程序从我自己实现的队列类中查看队列对象,所以我试图迭代它并打印出所有元素以查看错误而不更改队列。我怎么能这样做?

My Queue类(QueueLinkedList是名称):

public class QueueLinkedList<Customer> implements Queue<Customer> {

    Node first, last;

    public class Node {
        Customer ele;
        Node next;
    }

    public QueueLinkedList() {}

    public boolean isEmpty() {
        return first == null;
    }

    public QueueLinkedList<Customer> enqueue(Customer ele) {
        Node current = last;
        last = new Node();
        last.ele = ele;
        last.next = null;

        if (current == null) 
            first = last;
        else 
            current.next = last;

        return this;
    }

    public Customer dequeue() {
        if (isEmpty()) 
            throw new java.util.NoSuchElementException();

        Customer ele = first.ele;
        first = first.next;     
        return ele;
    }

    public Customer peek() {
        Customer ele = first.ele;
        return ele;
    }

2 个答案:

答案 0 :(得分:2)

您正在使用链接列表来实现您的队列。您可以迭代它,就像迭代任何链表一样。

public void iterate() {
    Node iterator = first;
    while(iterator != null) {
        Customer customer = iterator.ele;
        // do something with the customer
        iterator = iterator.next;
    }
}

编辑:如果您的用例需要返回迭代器,那么理想情况下您应该实现Iterable接口。在另一个答案中已经提到了该解决方案。为了将此答案扩展到您的用例,我提供以下代码。它可以工作,但它不是&#34;面向对象的&#34;这样做的方式。

public class QueueLinkedList<Customer> implements Queue<Customer> {

    private Node iterator;

    // ...

    public QueueLinkedList() {
        iterator = null;
        // ...
    }

    public Node iterator() {
        iterator = first;
        return iterator;
    }

    public boolean hasNext() {
        return iterator != null;
    }

    public Node next() {
        if(!hasNext()) {
            throw new NoSuchElementException();
        }
        Node next = iterator;
        iterator = iterator.next();
        return next;
    }

}

用法:

QueueLinkedList queue = new QueueLinkedList();
// ...
Node iterator = queue.iterator();
while(queue.hasNext()) {
    Node next = queue.next();
    Customer customer = next.ele;
    // do something with the customer
}

答案 1 :(得分:1)

您需要在队列中实现Animal,如下所示,这样您的队列可以像数组和其他Java集合一样迭代。

Iterable<Customer>

请注意,您声明类的方式import java.util.*; public class QueueLinkedList<Customer> implements Queue<Customer>, Iterable<Customer> { Node first, last; public class Node { Customer ele; Node next; } class Iter implements Iterator<Customer> { Node current = first; public boolean hasNext() { return current != null; } public Customer next() { if (!hasNext()) throw new NoSuchElementException(); Customer next = current.ele; current = current.next; return next; } public void remove() { throw new UnsupportedOperationException(); } } public QueueLinkedList() {} public boolean isEmpty() { return first == null; } public QueueLinkedList<Customer> enqueue(Customer ele) { Node current = last; last = new Node(); last.ele = ele; last.next = null; if (current == null) first = last; else current.next = last; return this; } public Customer dequeue() { if (isEmpty()) throw new java.util.NoSuchElementException(); Customer ele = first.ele; first = first.next; return ele; } public Iterator<Customer> iterator { return new Iter(); } } 是通用的类型参数,而不是类Customer。这实际上是一件好事,因为这意味着您可以将Customer类用于任何数据类型。为了明确QueueLinkedList是一个类型参数,您应该将每个Customer替换为一个由一个大写字母组成的类变量名,例如Customer

或者,如果您希望E始终是QueueLinkedList个对象的队列,则应将类声明更改为:

Customer
相关问题