I have an assignment which requires me to implement a generic priority queue from scratch, however I'm getting an error that I don't think makes any sense.
public class PriorityQueue<E> {
private ArrayList<E> items = new ArrayList<E>(0);
...
public <E extends Comparable<E>> void insert(E newItem){
if(numOfItems == 0){
items.add(newItem); //ERROR: The method add(E) in the type ArrayList<E>
is not applicable for the arguments (E)
rear++;
numOfItems++;
}else{
//INCOMPLETE
}
}
}
Accessing a Generic List. You can get and insert the elements of a generic List like this: List<String> list = new ArrayList<String>; String string1 = "a string"; list. add(string1); String string2 = list.
public <T extends Comparable<E>> void insert(E newItem){
Change the first 'E' to 'T', since the type parameter is hiding the original 'E'
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