I'm trying to make a priority queue ADT in Java using an array. I don't know Java very well but I'm getting the hang of it. The only problem I'm having is making an array of a class I made called QueueElement. I need the array to work with any data type so I create a new ArrayList of integers for example like this:
ArrayQueue<Integer> AQ = new ArrayQueue<Integer>(10);
The problem is I can't have an array of just a generic type T because I need to know the priority of each element in the array. So I made a class called ArrayElement which contains the data(type T) and the priority(type int). (priority can be between 1 and 5 inclusive). But it won't let me make that array. Here's my code:
public class ArrayQueue<T> {
private QueueElement[] arr;
private int total, front, rear, MAX_SIZE;
public class QueueElement {
T data;
int priority;
public QueueElement(T d, int p) {
data = d;
priority = p;
}
}
//constructor
public ArrayQueue(int size) {
//it won't let me cast this array to the type QueueElement
arr = (QueueElement[]) new Object[size];
//arr = new QueueElement[size]; doesn't work either
front = 0;
rear = -1;
total = 0;
MAX_SIZE = size;
}
}
If you have any idea on how I can do this it would help me a lot. Thanks.