对于每个我的自定义类将无法正常工作

时间:2009-12-01 02:58:24

标签: java

嘿大家我正在使用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);
        }
    }
}

2 个答案:

答案 0 :(得分:4)

我至少可以看到一个问题 仔细查看您的hasNext()方法。

你真的想增加变量counter吗?

答案 1 :(得分:2)

您的代码存在问题:

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

您已声明了os的引用,但您尚未指定任何内容。编译器不允许这样做。你必须这样做:

 OrderedSet os = new OrderedSet(10);

您的代码还有其他问题(@hallidave找到了一个),但这是第一个问题。

通常,当您遇到错误问题时,您应该提供的信息比“它不起作用”要多。准确的错误消息将很长一段时间来回答问题。我知道编译器错误消息对你来说意义不大,但是当你获得更多经验时,他们会。