public class PriorityQueue<E> {
private E[] array;
private int size;
private int front;
private int back;
private int numOfElements = 0;
private static int EMPTY = 0;
public <E> int insert(E input)
{
if (numOfElements + 1 <= size)
{
array[back] = input;
back++;
numOfElements++;
}
return 0;
}
For some reason, I'm getting a compilation error that says that I can't convert my input file, which is of type E, into type E. Why is this? Is it because It's not technically the same type E?
Yout are declaring two type parameters with the same name E. There is no need to do that. The type parameter in the class declaration PriorityQueue<E> is enough.
Change
public <E> int insert(E input)
to
public int insert(E input)
Remove the generic parameter from your insert method and it will compile. You do not need to be generic at the method level because you already have the type of your queue in the generic parameter of the class.
public class PriorityQueue<E> {
private E[] array;
private int size;
private int front;
private int back;
private int numOfElements = 0;
private static int EMPTY = 0;
public int insert(E input) {
if (numOfElements + 1 <= size) {
array[back] = input;
back++;
numOfElements++;
}
return 0;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With