-4

I am implementing a priority queue using a heap-array of defined "newsFeed" objects, which all have a String, an int of "likes", and an int of "age". The heap is prioritized by the number of "likes" each newsFeed object has, however when I use a removeMax on it, the removeMaxReorder method I wrote for it is giving me null pointers. As I'm debugging it, I was just curious as to if anyone knows of an algorithm that will get around this and basically reorder the array into descending order, thus still keeping the priority queue properties. I can't implement a comparable method, because I can't modify the newsFeed class, so I'm imagining a lot of for loops and statements like array[i].getLikes and a lot of >,< and =

4

1 回答 1

2

I assume you are not talking about the PriorityQueue class, and I am not sure if you are talking about an array or an ArrayList, but you don't need to implement Comparable as you can create a Comparator without changing the original class.

For your description it sounds like you want to reverse sort an array (or sort it in descending order) To do this I would use

NewsFeed[] newsFeed =
Arrays.sort(newsFeed, new MyDescendingPriorityOrderComparator());
于 2012-12-09T00:23:37.983 回答