ArrayQoreException with PriorityQueueItem <e>

时间:2016-03-23 10:39:35

标签: java

当我使用PriorityQueItem.add(E item)方法时,我不断收到ArrayStoreError。我的代码流程是......

  if (student.deductCoins(coins)) { //if the student has enough coins...

 PriorityQueueItem<Student> newPQI = new PriorityQueueItem<Student>          
 (coins); //...create a new PriorityQueueItem<E> object
          // using that amount for the priority

  newPQI.add(student); //add the student to the queue associated with this
                       //PriorityQueueItem object 

...在Course类中,然后我的PriorityQueItem.add(E项)是......

  public void add(E item) {getList().enqueue(item);}

...使用Queue<E>.enqueue(E item)方法将添加内容添加到new PriorityQueueItem<E>的队列中。每个PriorityQueueItem都有一个整数值,并在创建时分配给它Queue<E> queue; getList()获取队列。

根据堆栈跟踪,当到达queue[front]=item中的行Queue<E>.enqueue(E item)时会触发错误。根据API,当您尝试将错误类型的项添加到数组时会发生ArrayStoreException,但我无法看到此序列中的位置。任何帮助将不胜感激。

@Erin和@Thomas:

/ **  *  *用于表示存储在PriorityQueue中每个条目的对象的类。即,  * {@link PriorityQueue}的内部节点结构  *  * @author CS367  *  * @param  *存储在列表中的数据内容的泛型类型  * / 公共类PriorityQueueItem实现Comparable&gt; {

private int priority;
private Queue<E> queue;

public PriorityQueueItem(int priority) {

    this.priority = priority;
    this.queue = new Queue<E>();

}

public int getPriority() {
    // TODO
    return this.priority;
}

public Queue<E> getList() {
    // TODO
    return this.queue;
}

/**
 * Add an item to the queue of this PriorityQueueItem object
 * @param  
 * 
 * @param student
 *            item to add to the queue
 */
public void add(E item) {


    getList().enqueue(item);

}

/**
 * Compares this Node to another node on basis of priority
 * 
 * @param o
 *            other node to compare to
 * @return -1 if this node's priority is lesser, +1 if this nodes priority
 *         is higher after, else 0 if priorities are the same.
 */
@Override
public int compareTo(PriorityQueueItem<E> o) {

    int x = 0;

    if (this.getPriority() > o.getPriority())

        x = 1;

    if (this.getPriority() == o.getPriority())

        x = 0;

    if (this.getPriority() < o.getPriority())

        x = 1;

    return x;

}

}

/ **  *有序的物品收集,其中物品被添加到后面并被移除  * 从前面。  * / 公共类Queue实现QueueADT {

private static final int MAX_CAPACITY = 100;

private E[] queue;
private int size;
private int front;
private int rear;

// TODO
// You may use a naive expandable circular array or a chain of listnodes.
// You may NOT use Java's predefined classes such as ArrayList or
// LinkedList.

@SuppressWarnings("unchecked")
public Queue() {

    this.queue = (E[]) new Array [MAX_CAPACITY];
    this.size = 0;
    this.front = 0;
    this.rear = 0;

}

/**
 * Adds an item to the rear of the queue.
 * 
 * @param item
 *            the item to add to the queue.
 * @throws IllegalArgumentException
 *             if item is null.
 */

public void enqueue(E item) {



    if (item == null) {
        throw new IllegalArgumentException();
    }

    // if number of items exceeds size limits of the array
    // then expand the array

    if (!(size < queue.length - 1)) {

        this.expandCapacity();
    }


    queue[rear] = item;

    if (rear < queue.length - 1) {

        rear++;

    } else {
        rear = 0;
    }

    size++;
}

/**
 * Removes an item from the front of the Queue and returns it.
 * 
 * @return the front item in the queue.
 * @throws EmptyQueueException
 *             if the queue is empty.
 */
public E dequeue() {

    if (isEmpty()) {
        throw new EmptyQueueException();
    }

    E next = this.queue[front];

    queue[front] = null;

    if (this.front < queue.length - 1) {

        this.front++;

    } else {
        this.front = 0;
    }

    size--;

    return next;

}

/**
 * Returns the item at front of the Queue without removing it.
 * 
 * @return the front item in the queue.
 * @throws EmptyQueueException
 *             if the queue is empty.
 */
public E peek() {

    return this.queue[front];

}

/**
 * Returns true iff the Queue is empty.
 * 
 * @return true if queue is empty; otherwise false.
 */
public boolean isEmpty() {

    return this.size == 0;
}

/**
 * Removes all items in the queue leaving an empty queue.
 */
public void clear() {

    while (!isEmpty()) {

        this.dequeue();

    }

}

/**
 * Returns the number of items in the Queue.
 * 
 * @return the size of the queue.
 */
public int size() {
    return this.size;
}

@SuppressWarnings("unchecked")
private void expandCapacity() {

    E[] newQueue;

    newQueue = (E[]) Array.newInstance(PriorityQueueItem.class,
            this.size() * 2);

    for (int i = 0; i < queue.length; i++) {

        newQueue[i] = this.queue[i];

    }

    this.queue = newQueue;

}

}

0 个答案:

没有答案