I keep getting an ArrayStoreError when I use my PriorityQueItem.add(E item) meth
ID: 3680697 • Letter: I
Question
I keep getting an ArrayStoreError when I use my PriorityQueItem.add(E item) method. The flow of my code is...
if (student.deductCoins(coins)) {
PriorityQueueItem<Student> newPQI = new PriorityQueueItem<Student> (coins);
newPQI.add(student);
...in the Course class and then my PriorityQueItem.add(E item) is: public void add( E item) {getList().enqueue(E item);} where getList() returns the Queue<E> instance variable that is part of the PriorityQueueItem object "newPQI" and "item" is the value to be added to the queue. This method in turn uses the Queue<E>.enqueue(E item) method to add the add to the queue of new PQI. Based on the stack trace, the error is triggered when the line "queue[front]=item" in Queue<E>.enqueue(E item) is reached. According to the API, the ArrayStoreException occurs when you try to add the wrong type of an item to an array but I do not see where in this sequence where it happens. The constructor in PriorityQueueItem is "public PriorityQueueItem(int priority) {this.priority=priority; this.queue=new Queue<E>();} and the constructor in Queue<E> is "public Queue () {this.queue=(E[]) new Array[MAX_CAPACITY]; this.size=0; this.front=0; this.rear=0;} Any help will be appreciated.
Explanation / Answer
The java.util.PriorityQueue class is an unbounded priority queue based on a priority heap.Following are the important points about PriorityQueue:
Class declaration
Following is the declaration for java.util.PriorityQueue class:
public class PriorityQueue<E>
extends AbstractQueue<E>
implements Serializable
Parameters
Following is the parameter for java.util.PriorityQueue class:
Class constructors
S.N.
Constructor & Description
1
PriorityQueue()
This creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
2
PriorityQueue(Collection<? extends E> c)
This creates a PriorityQueue containing the elements in the specified collection.
3
PriorityQueue(int initialCapacity)
This creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
4
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
5
PriorityQueue(PriorityQueue<? extends E> c)
This creates a PriorityQueue containing the elements in the specified priority queue.
6
PriorityQueue(SortedSet<? extends E> c)
This creates a PriorityQueue containing the elements in the specified sorted set.
Class methods
S.N.
Method & Description
1
boolean add(E e)
This method inserts the specified element into this priority queue.
2
void clear()
This method removes all of the elements from this priority queue.
3
Comparator<? super E> comparator()
This method returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
4
boolean contains(Object o)
This method returns true if this queue contains the specified element.
5
Iterator<E> iterator()
This method returns an iterator over the elements in this queue.
6
boolean offer(E e)
This method inserts the specified element into this priority queue.
7
E peek()
This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
8
E poll()
This method retrieves and removes the head of this queue, or returns null if this queue is empty.
9
boolean remove(Object o)
This method removes a single instance of the specified element from this queue, if it is present.
10
int size()
This method returns the number of elements in this collection.
11
Object[] toArray()
This method returns an array containing all of the elements in this queue.
12
<T> T[] toArray(T[] a)
This method returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.
Description
The toArray(T[] a) method is used to return an array containing all of the elements in this queue.
Declaration
Following is the declaration for java.util.PriorityQueue.toArray() method.
public <T> T[] toArray(T[] a)
Parameters
Return Value
Exception
Example
The following example shows the usage of java.util.PriorityQueue.toArray()
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[]) {
// create priority queue
PriorityQueue < Integer > prq = new PriorityQueue < Integer > ();
// insert values in the queue
prq.add(6);
prq.add(9);
prq.add(5);
prq.add(64);
prq.add(6);
System.out.println ( "Priority queue values are: "+ prq);
// create arr1
Integer[] arr1 = new Integer[5];
// use toArrsy() method
Integer[] arr2 = prq.toArray(arr1);
System.out.println ( "Value in arr1: ");
for ( int i = 0; i<arr1.length; i++ ){
System.out.println ( "Value: " + arr1[i]) ;
}
System.out.println ( "Value in arr2: ");
for ( int i = 0; i<arr2.length; i++ ){
System.out.println ( "Value: " + arr2[i]) ;
}
}
}
compile and run the above program, this will produce the following result.
Priority queue values are: [5, 6, 6, 64, 9]
Value in arr1:
Value: 5
Value: 6
Value: 6
Value: 64
Value: 9
Value in arr2:
Value: 5
Value: 6
Value: 6
Value: 64
Value: 9
Note-the eabove explanation can be helpful for the given question.
S.N.
Constructor & Description
1
PriorityQueue()
This creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
2
PriorityQueue(Collection<? extends E> c)
This creates a PriorityQueue containing the elements in the specified collection.
3
PriorityQueue(int initialCapacity)
This creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
4
PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
5
PriorityQueue(PriorityQueue<? extends E> c)
This creates a PriorityQueue containing the elements in the specified priority queue.
6
PriorityQueue(SortedSet<? extends E> c)
This creates a PriorityQueue containing the elements in the specified sorted set.