I am asked to convert a program to use generic types in my array.
I have no idea where to start. I tried to convert the array to generic but I keep getting problems.
I tried to just convert the array to generic one by using queArray = (T[]) (new Comparable[maxSize]); but after I modified all variables to suppose to use generic types I keep getting errors.
What do I have to change here?
You want to declare that
Tmust implementComparable. This allows you to explicitly state that thePriorityQcan only support classes which can be compared for ordering. This is done like so:Now
queArrayshould benewed as an array ofTsince thePriorityQis ordering instances of typeT:Lastly, use the
Comparable.compareTomethod for your comparisons: