为什么我得错误找不到方法?

时间:2014-10-01 04:37:50

标签: java if-statement queue

我正在进行一项要求我使用队列的作业。代码的一部分直接来自我的书,但是我的ArrayUnbndQueue类的dequeue方法中出现了几个错误。

public class ArrayUnbndQueue<T> implements UnboundedQueueInterface<T>
{
    protected final int DEFCAP = 100; // default capacity
    protected T[] queue;              // array that holds queue elements
    protected int origCap;            // original capacity
    protected int numElements = 0;    // number of elements in the queue
    protected int front = 0;          // index of front of queue
    protected int rear = -1;          // index of rear of queue

    public ArrayUnbndQueue()
    {
        queue = (T[]) new Object [DEFCAP];
        rear = DEFCAP - 1;
        origCap = DEFCAP;
    }

    public ArrayUnbndQueue(int origCap)
    {
        queue = (T[]) new Object[origCap];
        rear = origCap - 1;
        this.origCap = origCap;
    }

    private void enlarge()
    {
        T[] larger = (T[]) new Object[queue.length + origCap];

        int currSmaller = front;
        for (int currLarger = 0; currLarger < numElements; currLarger++)
        {
            larger[currLarger] = queue[currSmaller];
            currSmaller = (currSmaller + 1) % queue.length;
        }

        // update instance variables
        queue = larger;
        front = 0;
        rear = numElements - 1;
    }

    public void enqueue(T element)
    {
        if (numElements == queue.length)
            enlarge();
        rear = (rear + 1) % queue.length;
        queue[rear] = element;
        numElements = numElements + 1;
    }

    public T dequeue()
    {
        if (isEmpty())
            throws new QueueUnderflowException("Dequeue attempted on empty queue.");
        else
            {
              T toReturn = queue[front];
              queue[front] = null;
              front = (front + 1) % queue.length;
              numElements = numElements -1;
              return toReturn;
            }
    }

    public boolean isEmpty()
    {
        return (numElements == 0);
    }

}

公共T deque方法完全从我的书中复制过来。 QueueUnderflowException类实际上没有显示在我的书中,所以我在我的界面和这个类中得到了其他错误,但我为此编写了代码

public class QueueUnderflowException extends RuntimeException
{
    public QueueUnderflowException()
    {
        super();
    }
    public QueueUnderflowException(String message)
    {
        super(message);
    }
}

Netbeans给我一个错误,它无法找到方法QueueUnderflowException,否则没有if和缺少return语句。

我试图在throws异常行周围添加括号,如下面的代码所示。这将删除我的&#34;否则如果&#34;错误,我可能会将return语句移到else语句的外面,但我仍然无法找到方法错误

public T dequeue()
        {
            if (isEmpty())
            {
                throws new QueueUnderflowException("Dequeue attempted on empty queue.");
            }
            else
                {
                  T toReturn = queue[front];
                  queue[front] = null;
                  front = (front + 1) % queue.length;
                  numElements = numElements -1;
                  return toReturn;
                }
        }

3 个答案:

答案 0 :(得分:2)

throws new QueueUnderflowException("Dequeue attempted on empty queue.");

应该是

throw new QueueUnderflowException("Dequeue attempted on empty queue.");

答案 1 :(得分:0)

您需要更改public T dequeue()

public T dequeue() {
  if (isEmpty())
  throws new QueueUnderflowException("Dequeue attempted on empty queue.");//throw 
                                                                     //not throws
  // this should be throw not throws
}

您正在从此dequeue方法中抛出异常。为此,您需要在方法签名中使用throws

public T dequeue() throws QueueUnderflowException{
 throw new QueueUnderflowException("Dequeue attempted on empty queue.");
}

答案 2 :(得分:0)

不确定是否会解决所有错误,但您需要在此类声明中使用throw而不是throws

            throws new QueueUnderflowException("Dequeue attempted on empty queue.");