For the benefit of the reader, i have added the explanation of the program (as per K&B) below :
Let's look at this in detail. The first for loop iterates through the ia array, and uses the offer() method to add elements to the PriorityQueue named pq1. The second for loop iterates through pq1 using the poll() method, which returns the highest priority entry in pq1 AND removes the entry from the queue. Notice that the elements are returned in priority order (in this case, natural order). Next, we create a Comparator—in this case, a Comparator that orders elements in the opposite of natural order. We use this Comparator to build a second PriorityQueue, pq2, and we load it with the same array we used earlier. Finally, we check the size of pq2 before and after calls to peek() and poll(). This confirms that peek() returns the highest priority element in the queue without removing it, and poll() returns the highest priority element, AND removes it from the queue. Finally, we review the remaining elements in the queue.
Henry Wong wrote:
It looks like you didn't copy the code correctly -- those are syntax errors. For example, the first one is caused by missing braces.
Henry
got it, sorry my mistake. But how do i interpret compile and run-time errors (except the simple ones). Is there any manual/trouble shooting guide for this, for example if you get xyz error - in plain english your problem is abc...
You should can always go down the compiler error messages you get, fixing one by one. I saw that in the Java7 the readability of the compiler messages have been improved.
The other thing to consider is how you are formatting your code. In the code you posted your use of indentations is inconsistent. To some degree editors can help you find bugs, and to some degree IDEs can too, but it's also good for you to format your code so that when you (or other programmers) look at it later, it's as easy to read as possible. As an example you had something like:
for(....)
stmt1;
stmt2;
Without curly braces, only stmt1 will be a part of the for loop. Because you didn't use curly braces AND you didn't indent stmt1, this is an easy bug for the eye to miss. There is another discussion about whether you should ALWAYS use curly braces, but I'll leave that for another day.
hth,
Bert
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mohamed Sanaulla wrote:You should can always go down the compiler error messages you get, fixing one by one. I saw that in the Java7 the readability of the compiler messages have been improved.
Where can i see examples of this ?
SCJP 6. Learning more now.
Don't get me started about those stupid light bulbs.