2009-12-01 70 views
-2

嘿,大家我正在使用Java。
所以我正在寫一個叫做OrderedSet的類。它是一個集合和一個隊列之間交叉的類。換句話說,它是一個沒有任何重複的隊列。所以我知道我必須實現Iterable接口,並編寫一個迭代器方法。爲了編寫該方法,我必須實現接口Iterator對於每個與我的自定義類將無法正常工作

package comp345; 

import java.util.Iterator; 
import java.util.NoSuchElementException; 

class OrderedSet implements Iterable<Object> { 
    private Object[] queue; // This is how the OrderedSet is represented, a 
          // queue of Objects 
    private int counter; // Counter to keep track of current position of queue 
    private int queueSize; // Keep track of the queue; 

    // Constructor 
    public OrderedSet(int size) { 
     this.queue = new Object[size]; 
     this.counter = 0; 
     this.queueSize = size - 1; 

     // Instantiate each instance of the object in the object array 
     for (int i = 0; i < queue.length; i++) { 
      queue[i] = new Object(); 
     } 
    } 

    /** 
    * Ensures that this collection contains the specified element. If it is not 
    * already in the collection it is added it to the back of the queue. 
    * 
    * @param e 
    *   element whose presence in this collection is to be ensured 
    * @return true if this collection changed as a result of the call 
    * @throws NullPointerException 
    *    if the specified element is null 
    */ 
    boolean add(Object i) { 
     if (i == queue[counter]) { 
      return false; 
     } else if (i == null) 
      throw new NullPointerException(); 
     else { 
      // Add Object to back of Queue 
      queue[counter] = i; 
      return true; 
     } 
    } 

    /** 
    * Removes all of the elements from this collection. The collection will be 
    * empty after this method returns. 
    */ 
    void clear() { 
     for (int i = 0; i < queue.length; i++) { 
      queue[i] = null; 
     } 
    } 

    /** 
    * Returns true if this collection contains no elements. 
    * 
    * @return true if this collection contains no elements 
    */ 
    boolean isEmpty() { 
     if (queue[0] == null) 
      return true; 
     else 
      return false; 
    } 

    /** 
    * Retrieves, but does not remove, the head of this queue, or returns null 
    * if this queue is empty 
    * 
    * @return the head of this queue, or null if this queue is empty 
    */ 
    Object peek() { 
     if (queue[counter] != null) 
      return queue[counter]; 
     else 
      return null; 
    } 

    /** 
    * Retrieves and removes the head of the queue. 
    * 
    * @return the head of this queue 
    * @throws NoSuchElementException 
    *    if this queue is empty 
    */ 
    Object remove() { 
     if (queue[0] != null) { 
      Object temp = queue[0]; 

      queue[0] = queue[1]; 

      return temp; 

     } else 
      throw new NoSuchElementException(); 
    } 

    public class SetIterator implements Iterator<Object> { 
     private int counter; 

     public SetIterator() { 
      this.counter = 0; 
     } 

     @Override 
     public Object next() { 
      counter++; 

      if (queueSize == counter) 
       return null; 

      else if (queue[counter] != null) 
       return (Object) queue[counter]; 

      else 
       throw new NoSuchElementException(); 
     } 

     @Override 
     public boolean hasNext() { 
      counter++; 

      if (queueSize < counter) 
       return false; 
      else if (queue[counter] != null) 
       return true; 

      return false; 
     } 

     @Override 
     public void remove() { 
      throw new UnsupportedOperationException(); 
     } 
    } 

    @Override 
    public Iterator<Object> iterator() { 
     return new SetIterator(); 
    } 

    public static void main(String args[]) { 
     OrderedSet os; 
     os.add("hello"); 
     os.add(4); 
     os.add("bye"); 

     for (Object o : os) { 
      System.out.println(o); 
     } 
    } 
} 
+1

那麼......你的問題到底是什麼? – 2009-12-01 03:00:40

+0

請顯示無法使用的「foreach」代碼。 – 2009-12-01 03:02:38

+0

好吧,我編輯程序通過添加主顯示哪裏不工作。 – ProxyProtocol 2009-12-01 03:12:52

回答

4

我可以看到至少有一個問題。
仔細看看你的hasNext()方法。

你真的想增加變量counter嗎?

2

與您的代碼的問題是在這裏:

OrderedSet os;  
os.add("hello"); 

您已經聲明OS的參考,但是你有沒有分配任何東西。編譯器不會允許這樣做。你必須這樣做:

OrderedSet os = new OrderedSet(10); 

你的代碼還有其他問題(@hallidave找到了一個),但這是第一個問題。

一般來說,當您遇到錯誤問題時,您應該詢問更多信息,而不是「無效」。確切的錯誤信息將很快回答這個問題。我知道編譯器的錯誤信息對你來說意義不大,但是當你獲得更多的經驗時,他們會。

相關問題