2017-04-17 12 views
0

私は自分自身を実装したキュークラスからキューオブジェクトを覗いているプログラムの一部をデバッグしようとしていますので、それを繰り返してすべての要素を出力してキューを変更せずに何が問題になっているかを確認しようとしています。これどうやってするの?キューを反復処理する方法私は自分自身を実装しましたか?

マイキュークラス(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; 
    } 
+0

:あなたはQueueLinkedListは常にCustomerオブジェクトのキューになりたい場合は

あるいは、あなたはにクラス宣言を変更する必要があります。 'QueueLinkedList()'コンストラクタは 'QueueLinkedList'クラスになければなりません。そこでは、うまくコンパイルできません。 – davidxxx

+0

これはQueueLinkedListクラスです –

+2

'Iterable'インターフェースを実装し、そのための' Iterator'を作成する必要があります。 –

答えて

2

あなたのキューを実装するためにリンクされたリストを使用しています。リンクされたリストを反復処理するのと同じように、反復処理を行うことができます。

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

編集:ユースケースがイテレータを返す必要がある場合は、理想的にはIterableインターフェイスを実装する必要があります。その解決策はもう一つの答えで言及されています。この答えをユースケースに拡張するために、私は以下のコードを提供しています。それはうまくいくでしょうが、それをする "オブジェクト指向"の方法ではありません。

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 
} 
+0

ありがとうございます。しかし今は、Iteratorを返すメソッドを使う必要があると言います。public Iterator iterate()のようなものです。リターン・ステートメントとして追加する必要があるのは何ですか?私は(イテレータ)イテレータを返しますか?またはnullを返すのですか? –

1

あなたは以下に示すように、あなたのキューが配列や他のJavaコレクションと同じ反復することができるように、あなたのキューにIterable<Customer>を実装する必要があります。あなたのクラスを宣言している方法は、Customerはジェネリック型パラメータ、ないクラス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(); 
    } 
} 

注意。これは実際には良いことです。つまり、あなたのQueueLinkedListクラスをどんなデータ型でも使うことができます。 Customerがタイプパラメータであることを明確にするためには、のような単一の大文字で構成されるクラス変数名でCustomerのすべてのオカレンスを置き換える必要があります。それはいくつかのコードをミス

public class QueueLinkedList 
    implements Queue<Customer>, Iterable<Customer> 
関連する問題